Cakephp: how to get an array of elements from a web form

In my cakephp form, I have the following code

<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
<p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>

I am trying to get values ​​from a set of text input fields, the number of text fields can be set by the user, so I can not specify the individual names of each text field, but how can I get the values ​​from my controller to insert data into the db table

thank

+3
source share
3 answers

You can leave the form as is (and use offers from @Wizzard and @Lee), but it is best to use an incremental variable to build the list. i.e:.

for($i=0;$i<$option_number;$i++){
   echo $form->input("MyModel.{$i}.option");
}

Thus, your variable after publishing the form will look like this:

data [MyModel] [0] [option] = ''  data MyModel [option] = ''  data [MyModel] [2] [option] = ''  ... ...

:

print_r ($ - > );

saveAll() ( saveAll )

+3

: option[]. . php . , CakePHP :

$this->params['form']['option'][0]
$this->params['form']['option'][1]
... and so on ...
+1

, $this- > params ['form'] .. $this- > data​​p >

var_dump ($ this); , .

0

Source: https://habr.com/ru/post/1772329/


All Articles