我正在尝试解决我无法理解的表单提交问题。当我第一次提交表单时,更改下拉列表的值后,$this->request->data
数组为空。如果我再次提交,我会看到我所期望的。每次我更改表单上的任一下拉菜单时都会发生这种情况。
这是表格:
<?php
echo $this->Form->create('Refine', array('url' => '/ServiceDirectoryResults/refine'));
echo $this->Form->input('state', array(
'type' => 'select',
'label' => 'State',
'options' => $all_states,
'selected' => array('state_selected', $state_selected),
'id' => 'state',
));
echo $this->Form->input('solution', array(
'type' => 'select',
'label' => 'Solution',
'options' => $solutions,
'selected' => array('selected', $solution),
'id' => 'solutions',
));
echo $this->Form->input('region', array(
'before' => '<fieldset id="Region">',
'multiple' => 'checkbox',
'options' => $regions,
'selected' => $reg_selected,
'after' => '</fieldset>'
));
echo $this->Form->input('tags', array(
'before' => '<fieldset id="TagBox">',
'multiple' => 'checkbox',
'options' => $narrow,
'selected' => $tag_selected,
'after' => '</fieldset>'
));
echo $this->Form->end('Refine Search');
?>
表格渲染得很好。如果更改了状态或解决方案下拉列表并提交了表单,则$this->request->data
数组为空。如果我第二次提交,而不更改任何内容,则数组包含我希望看到的内容。
在我的控制器中,我有
if(isset($this->request->data['Refine']['state']))
{
$state = $this->request->data['Refine']['state'];
}
显然,如果数组为空,我在第一次提交表单时在状态变量中什么也得不到。
如果有人能对这种行为有所了解,我将不胜感激。我在创建表单时做错了什么?
这里要求的是与此表单一起使用的 js。这个想法是,如果“全部”复选框是为控制器中的区域和标签创建的第一个复选框,它只负责设置或清除复选框。
$(document).ready(function(){
$("#RefineRegion0").click(function(){
if ($("#Region #RefineRegion0").is(':checked')) {
$("#Region input[type=checkbox]").each(function (e) {
$(this).prop("checked", true);
});
} else {
$("#Region input[type=checkbox]").each(function (e) {
$(this).prop("checked", false);
});
}
});
$("#RefineTags0").click(function(){
if ($("#TagBox #RefineTags0").is(':checked')) {
$("#TagBox input[type=checkbox]").each(function (e) {
$(this).prop("checked", true);
});
} else {
$("#TagBox input[type=checkbox]").each(function (e) {
$(this).prop("checked", false);
});
}
});
$("#RefineViewForm").submit(function(){
if($('#state').val() == "" || $('#solutions').val() == ""){
alert("Please select a State and Solution before continuing")
}
});
});
希望有帮助