Surely a foolish error, but I can't see it
I have a form (greatly simplified):
<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>
And I have a class "EventForm" to process the form. The main method, process() looks like this:
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;
}
When the form is submitted, this happens: $form->process('event_submit');
For some reason though, isset($_POST['event_submit'])
always evaluates to FALSE. Can anyone see wh开发者_开发知识库y?
ETA: after working through the suggestions, it appears that JQuery.validate() is having an unexpected effect on the form submission. All the fields are submitted except the submit button. This appears to be the fault of this JQuery:
$("form[name='event_form']").validate({
submitHandler: function(form) {
form.submit();
}
}};
Any thoughts on how to make sure the submit button value gets sent?
Change your JQuery to this:
$("form[name='event_form']").validate({
submitHandler: function(form) {
$("form[name='event_form'] input[name='event_submit']").click()
}
}};
do a print_r on the $_POST array and see whats being submitted - it should output the whole array e.g.
print_r($_POST);
I broke your code out into a even simpler PHP file:
<?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" and "bwah" displayed as expected. Make sure that your class is being instantiated properly, that you're actually passing "event_submit" to it, and that some other piece of code isn't wiping out $_POST before you get a chance to read it.
精彩评论