I had created a input fields in the View Page like
我在View Page中创建了一个输入字段
<div class="input text">
<label for="2">First Name (required) </label>
<input type="text" value="" style="width: 300px;" id="2" class="required" name="First Name"/>
</div>
<div class="input text">
<label for="5">Emailid</label>
<input type="text" value="" style="width: 300px;" id="5" name="Emailid"/><
/div>
<div class="input text">
<label for="6">Mobile Number</label>
<input type="text" value="" style="width: 30px;" id="6" name="Mobile Number"/>
<input type="text" value="" style="width: 30px;" id="6-1" name="Mobile Number"/>
<input type="text" value="" style="width: 70px;" id="6-2" name="Mobile Number"/>
</div>
these are all generated in the Page .I am retrieving the content values form this page in my CakePHp controller using
这些都是在Page中生成的。我正在使用CakePHp控制器从此页面检索内容值
foreach ($_POST as $key => $value):
echo $key;echo $value;
endforeach;
For all the above fields i am getting the correct answer like First_Name=A&Emailid=aruna@tcs.com&Mobile_Number=3
对于以上所有字段,我得到正确的答案,如First_Name=A&Emailid=aruna@tcs.com&Mobile_Number=3
But for my Mobile Number field alone i am getting the value of the last id like 6-2.Why so .? HOw to retrieve the full value of the mobile number which is given in the three fields (6,6-1,6-2) ??Please suggest me.
但是对于我的手机号码字段,我得到的最后一个id的值就像6-2。为什么呢?请求检索三个字段(6,6-1,6-2)中给出的手机号码的全部值?请建议我。
Edit :
foreach ($_POST as $key => $value):
$mobile_number = implode('', $_POST['number']);
echo $mobile_number;
$this->data['Result']['form_id']=$formid;
$this->data['Result']['label']=Inflector::humanize($key);
$this->data['Result']['value']=$value;
$this->Form->submitForm($this->data);
endforeach;
i am saving the key and its values using like above (ie.., The controller doesn't know what are all fields are in the Fill Page). The fillpage may/maynot contain the field Phone Number.How do i assure that the page has Phonenumber field and to save its value with its Key.
我正在使用如上所述保存密钥及其值(即..,控制器不知道填充页面中的所有字段是什么)。填充可能/可能不包含字段电话号码。如何确保页面具有Phonenumber字段并使用其密钥保存其值。
Note: $key is the Fieldname and $value is my value of the field . Only for Phonenumber Field the values are array and in all other cases it is only a single value.
注意:$ key是Fieldname,$ value是我的字段值。仅对于Phonenumber字段,值是数组,在所有其他情况下,它只是单个值。
1 个解决方案
#1
If you're using Cake, you really ought to be learning and using Cake.
如果您正在使用Cake,那么您真的应该学习并使用Cake。
Just a quick run-down of the flow in Cake:
只是快速破坏Cake中的流程:
Form page -> Submit
^ |
| v
| Controller // Form data is in $this->data
| |
| v
| $this->Model->save($this->data); -> Model
| |
| v
-------- Controller <----------------------- Data validation
validation no good |
| validation okay
v
Controller <----------------------- Actually save data
|
v
Go somewhere else
What this means for you:
这对你意味着什么:
$this->data
is central for Forms to work correctly. DO NOT TOUCH $_POST! Use the Cake methods for creating Forms and use the same names for fields as are used in the database. That facilitates a lot of the Cake "automagic". You don't hand-assemble an array to pass to the database, you use the same names for everything from start to finish and just pass the array around. $this->data
will automatically be passed back from the Controller to the View (i.e. the Form), if you use the Cake FormHelper the fields will automatically be filled back in from it.
$ this-> data是Forms正常工作的核心。请勿触摸$ _POST!使用Cake方法创建表单,并使用与数据库中使用的字段相同的名称。这有利于很多蛋糕“自动化”。你没有手工组装一个数组来传递给数据库,你从头到尾使用相同的名称,只是传递数组。 $ this-> data将自动从Controller传递回View(即Form),如果你使用Cake FormHelper,字段将自动从中填充。
You should minimize the work you have to do between submitting the form and saving the data. Ideally, your controller only looks like this:
您应该尽量减少提交表单和保存数据之间的工作。理想情况下,您的控制器只是这样:
if (!empty($this->data)) {
if ($this->Model->save($this->data)) {
$this->redirect(array('action' => 'next_step'));
}
}
That's all the code you need, and it will
这就是你需要的所有代码,它会
- display the page
- handle form submissions
- validate the input
- fill the form fields back in if validation failed
- save the data if validation was successful
- redirect to the next step if the data was saved successfully
显示页面
处理表单提交
验证输入
如果验证失败,请填写表单字段
验证成功后保存数据
如果数据已成功保存,则重定向到下一步
If your form fields don't correspond 1:1 with your database fields, tweak only the bits you need to between if(!empty($this->data))
and $this->Model->save($this->data)
:
如果表单字段与数据库字段不对应1:1,则只调整if(!empty($ this-> data))和$ this-> Model-> save($ this->之间所需的位数。数据):
if (!empty($this->data)) {
$this->data['Model']['mobile_number'] = implode('-', $this->data['Model']['mobile_number']);
if ($this->Model->save($this->data)) {
$this->redirect(array('action' => 'next_step'));
}
}
If you want to validate the submitted form data for completeness, you only fill in the appropriate rules in the Model. Whenever you issue a $this->Model->save()
, the model will automatically check the rules before saving and return false
if they're not met.
如果要验证提交的表单数据的完整性,只需在模型中填写相应的规则。每当您发出$ this-> Model-> save()时,模型将在保存前自动检查规则,如果不满足则返回false。
#1
If you're using Cake, you really ought to be learning and using Cake.
如果您正在使用Cake,那么您真的应该学习并使用Cake。
Just a quick run-down of the flow in Cake:
只是快速破坏Cake中的流程:
Form page -> Submit
^ |
| v
| Controller // Form data is in $this->data
| |
| v
| $this->Model->save($this->data); -> Model
| |
| v
-------- Controller <----------------------- Data validation
validation no good |
| validation okay
v
Controller <----------------------- Actually save data
|
v
Go somewhere else
What this means for you:
这对你意味着什么:
$this->data
is central for Forms to work correctly. DO NOT TOUCH $_POST! Use the Cake methods for creating Forms and use the same names for fields as are used in the database. That facilitates a lot of the Cake "automagic". You don't hand-assemble an array to pass to the database, you use the same names for everything from start to finish and just pass the array around. $this->data
will automatically be passed back from the Controller to the View (i.e. the Form), if you use the Cake FormHelper the fields will automatically be filled back in from it.
$ this-> data是Forms正常工作的核心。请勿触摸$ _POST!使用Cake方法创建表单,并使用与数据库中使用的字段相同的名称。这有利于很多蛋糕“自动化”。你没有手工组装一个数组来传递给数据库,你从头到尾使用相同的名称,只是传递数组。 $ this-> data将自动从Controller传递回View(即Form),如果你使用Cake FormHelper,字段将自动从中填充。
You should minimize the work you have to do between submitting the form and saving the data. Ideally, your controller only looks like this:
您应该尽量减少提交表单和保存数据之间的工作。理想情况下,您的控制器只是这样:
if (!empty($this->data)) {
if ($this->Model->save($this->data)) {
$this->redirect(array('action' => 'next_step'));
}
}
That's all the code you need, and it will
这就是你需要的所有代码,它会
- display the page
- handle form submissions
- validate the input
- fill the form fields back in if validation failed
- save the data if validation was successful
- redirect to the next step if the data was saved successfully
显示页面
处理表单提交
验证输入
如果验证失败,请填写表单字段
验证成功后保存数据
如果数据已成功保存,则重定向到下一步
If your form fields don't correspond 1:1 with your database fields, tweak only the bits you need to between if(!empty($this->data))
and $this->Model->save($this->data)
:
如果表单字段与数据库字段不对应1:1,则只调整if(!empty($ this-> data))和$ this-> Model-> save($ this->之间所需的位数。数据):
if (!empty($this->data)) {
$this->data['Model']['mobile_number'] = implode('-', $this->data['Model']['mobile_number']);
if ($this->Model->save($this->data)) {
$this->redirect(array('action' => 'next_step'));
}
}
If you want to validate the submitted form data for completeness, you only fill in the appropriate rules in the Model. Whenever you issue a $this->Model->save()
, the model will automatically check the rules before saving and return false
if they're not met.
如果要验证提交的表单数据的完整性,只需在模型中填写相应的规则。每当您发出$ this-> Model-> save()时,模型将在保存前自动检查规则,如果不满足则返回false。