cakephp : how to get an array of elements from a web form
In my cakephp form I have 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>开发者_JAVA百科
I am trying to get values from a set of input text boxes, the number of text boxes can be set by the user, so cant give individual names of each text box, but How can I get values from my controller to insert data to db table
Thank you
You can leave the form as it is (and use suggestions from @Wizzard and @Lee), but the best practice is to use an incrementing variable to construct the list. i.e.:
for($i=0;$i<$option_number;$i++){
echo $form->input("MyModel.{$i}.option");
}
This way your variable after posting the form will look like:
data[MyModel][0][option] = 'the value' dataMyModel[option] = 'the value' data[MyModel][2][option] = 'the value' ... and so on...
In the controller you can access the posted data by:
print_r($this->data);
Take a look saveAll() (search for saveAll in your browser and look for suggested data structure)
your input fields are all named the same thing: option[]
. This is good. It causes php to automatically turn them into an array when the request is loaded in. So you can get them in your CakePHP controller like this:
$this->params['form']['option'][0]
$this->params['form']['option'][1]
... and so on ...
Pretty sure they're in the array $this->params['form'] in the controller.. or $this->data
In the method of your controller, do a var_dump($this); and you'll see where they show up
精彩评论