doqw89029 2015-04-12 16: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 17: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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化