我有一张表格(大大简化):
<form action='http://example.com/' method='post' name='event_form'>
<input type='text' name='foo' value='3'/>
<input type='submit' name='event_submit' value='Edit'/>
</form>
我有一个“EventForm”类来处理表单。主要方法process()如下所示:
public function process($submitname=false){
$success=false;
if ($submitname && isset($_POST[$submitname])){ //PROBLEM: isset($_POST[$submitname] is always FALSE for some reason
if($success=$this->ruler->validate()){//check that dependancies are honoured and data types are correct
if($success=$this->map_fields()){//divide input data into Event, Schedule_Element and Temporal_Expression(s)
$success=$this->eventholder->save();
}
}
} else {//get the record from the db based on event_id or schedule_element_id
foreach($this->gets as $var){//list of acceptable $_GET keys
if(isset($_GET[$var])){
if($success= $this->__set($var,$_GET[$var])) break;
}
}
}
$this->action=(empty($this->eventholder->event_id))? ADD : EDIT;
return $success;
}
提交表单时,会发生这种情况:$form->process('event_submit');
出于某种原因,isset($_POST['event_submit'])
始终评估为FALSE。谁能明白为什么?
ETA:在完成建议后,似乎JQuery.validate()对表单提交产生了意想不到的影响。除提交按钮外,所有字段都已提交。这似乎是这个JQuery的错误:
$("form[name='event_form']").validate({
submitHandler: function(form) {
form.submit();
}
}};
有关如何确保提交按钮值的信息?
答案 0 :(得分:2)
在$ _POST数组上执行print_r并查看提交的内容 - 它应该输出整个数组,例如。
print_r($_POST);
答案 1 :(得分:2)
我把你的代码分成了一个更简单的PHP文件:
<?php
function process($submitname=false){
echo 'erg<br>';
$success=false;
if ($submitname && isset($_POST[$submitname])){ //PROBLEM: isset($_POST[$submitname] is always FALSE for some reason
echo 'bwah';
}
}
process("event_submit");
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="event_form">
<input type="text" name="foo"/>
<input type="submit" name="event_submit" value="Edit"/>
</form>
“erg”和“bwah”按预期显示。确保您的类正在被实例化,您实际上正在向它传递“event_submit”,并且在您有机会阅读之前,其他一些代码并没有消除$ _POST。
答案 2 :(得分:2)
将您的JQuery更改为:
$("form[name='event_form']").validate({
submitHandler: function(form) {
$("form[name='event_form'] input[name='event_submit']").click()
}
}};