doqw89029 2015-04-12 08:46
浏览 50
已采纳

too long

first: yes I know these SO questions cakephp-checkbox-multiple-select-just-sends-the-value-of-last-selected-checkbox and multiple-inputs-with-same-name-through-post-in-php . But both threads don't answer the same question I have.

My problem: I'm creating a view for a "route of planets". I now want to create a table where all of the "routeplanets" are listed with a checkbox at the end. Around the table there is a form to (thats my idea) to send an list/array of ids to remove them from the route.

I did several attemps but I don't get the right data in my request.

Base info: The table is build using a foreach-loop

- foreach($routePlanets as $routeplanet)
  - $class = $this->Html->cycle('even', 'odd')

  %tr{:class => "#{$class}"}
    %td.col1
      = [...]
    %td.col2
      = [...]          
    %td.colremove
      = # this is the line where to create the checkbox     

First attempt: Checkbox for every row

%td.remove
  = $this->Form->checkbox('id', array('value' => $routeplanet['RoutePlanet']['id'], 'hiddenField' => false))

As I assumed when writing this, this will always give me the last value. For example this is the generated HTML

// First row
<td class="remove"><input type="checkbox" name="data[Routeplanet][id]"  value="1"/></td>

// Second row
<td class="remove"><input type="checkbox" name="data[Routeplanet][id]"  value="3"/></td>

When I select both rows I get the following request, showing that only the last row is transmitted (or all before the last are overwritten):

var_dump($this->request->data['Routeplanet']);
// shows 
array(1) {
  ["id"]=>
  string(1) "3"
}

When accessing the data i get a "valid" result with the last ID:

$planetIds =  $this->request->data['Routeplanet']['id'];
var_dump($planetIds); 
// shows: string(1) "3"

Second attemp: Then I read in one of the two SO-question (see above) to use name=id[], so I changed my row to

%td.remove
  = $this->Form->checkbox('id', array('value' => $routeplanet['RoutePlanet']['id'], 'hiddenField' => false))

This results in the following HTML

// First row
<td class="remove"><input type="checkbox" name="data[Routeplanet][id[]]"  value="1"/></td>

// Second row
<td class="remove"><input type="checkbox" name="data[Routeplanet][id[]]"  value="3"/></td>

Selecting both results in

var_dump($this->request->data['Routeplanet']);
// shows 
array(1) {
  ["id["]=>
  string(1) "3"
}

When accessing the data i get a "valid" result with the last ID:

$planetIds =  $this->request->data['Routeplanet']['id[]'];
var_dump($planetIds); 
// shows: NULL

So I tried the second suggestion of the SO-question (see above), using select with multiple => checkbox option:

- $options[] = $routeplanet['RoutePlanet']['id']
= $this->Form->select('id', $options, array('multiple' => 'checkbox', 'hiddenField' => false))

But this results in false HTML

// First row
<td class="remove">
<div class="checkbox"><input type="checkbox" name="data[Routeplanet][id][]" value="0" id="0" /><label for="0">1</label></div>
</td>

// Second row
<td class="remove">
<div class="checkbox"><input type="checkbox" name="data[Routeplanet][id][]" value="0" id="0" /><label for="0">1</label></div>
<div class="checkbox"><input type="checkbox" name="data[Routeplanet][id][]" value="1" id="1" /><label for="1">3</label></div>
</td>

As you can see a) in the second row, the first is also added (because of $options[]) and b) the values are not the $routeplanet['RoutePlanet']['id'] - they are only used for labels (which I don't want by the way). I could break here as the layout and the HTML is wrong, but let me show you the even false request:

array(1) {
  ["id"]=>
  array(3) {
    [0]=>
    string(1) "0"
    [1]=>
    string(1) "0"
    [2]=>
    string(1) "1"
  }
}

I then thought about doing another loop to fill the options but I already assumed that then all rows will have all options. And I was right:

- foreach($routePlanets as $routeplanet)
  - $options[] = $routeplanet['RoutePlanet']['id']

- foreach($routePlanets as $routeplanet)
  %td.remove
    = $this->Form->select('id', $options, array('multiple' => 'checkbox', 'hiddenField' => false))

results in the following HTML for each row:

<td class="remove">
<div class="checkbox"><input type="checkbox" name="data[Routeplanet][id][]" value="0" id="0" /><label for="0">1</label></div>
<div class="checkbox"><input type="checkbox" name="data[Routeplanet][id][]" value="1" id="1" /><label for="1">3</label></div>
</td>    

So my question now is: How can I build a valid form, that gives me the option to mark a checkbox in each row, resulting that each marked row is in the sent request I acces in my controller?

Thanks in advance

展开全部

  • 写回答

1条回答 默认 最新

  • dongyue9860 2015-04-14 09:33
    关注

    The link provided by ndm was very helpfull - thank you! My mistake was that I only checked "checkbox" and "select" (with multiple-option) in the documentation but totally forget about the "normal" input.

    This is how I did it in the end:

    Form:

    - # MyModel as Helpername for easier access to the array
    - $fieldname = 'MyModel.' . $i . '.id'
    = $this->Form->input($fieldname, array('value' => $routeplanet['RoutePlanet']['id'], 'type' => 'checkbox', 'label' => false, 'hiddenField' => false))
    

    Controller:

    $MyModelArray = $this->request->data['MyModel'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部