cakephp:如何从Web窗体中获取元素数组 [英] cakephp : how to get an array of elements from a web form

查看:107
本文介绍了cakephp:如何从Web窗体中获取元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的cakephp表单中,我有以下代码

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>

我试图从一组输入文本框中获取值,文本框的数量可以是由用户设置,所以不能给每个文本框的个别名称,但如何从我的控制器获取值插入数据到db表

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

谢谢

推荐答案

您可以保留表单,并使用@Wizzard和@Lee的建议,但最好的做法是使用递增变量以构造列表。即:

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] ='值'
data MyModel [option] ='值'
data [MyModel] [2] [option] ='值'
.. ...等等...

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);

print_r($this->data);

查看 saveAll()(在浏览器中搜索saveAll并查找建议的数据结构)

Take a look saveAll() (search for saveAll in your browser and look for suggested data structure)

这篇关于cakephp:如何从Web窗体中获取元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆