views:

151

answers:

3

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 why?

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?

+2  A: 

do a print_r on the $_POST array and see whats being submitted - it should output the whole array e.g.

 print_r($_POST);
matpol
ok, really weird thing happened. The form is initially validated via JQuery.validate(), then submitted. When I output $_POST without regard for headers, it turned the JQuery off (expected), and my submit button had a value. Then when I moved the `print_r($_POST)` somewhere where it doesn't screw up JQuery, the submit button still had a value EVEN THOUGH previously when I did `print_r($_POST)` from that position, I was getting nothing. Maybe it's some weird combo of JQuery and caching causing the problem?
dnagirl
+1 always a good idea to double check whats coming in
thetaiko
@thetaiko: I agree. And I had. But I wasn't getting what I should. Then I moved the `print_r($_POST)` to a different place in the code and got the expected. Then I reverted and still got the expected.
dnagirl
@dnagirl - check `$_POST` at the very top of your script. If its there, then something else in your script is changing the value. If its not, then its a problem with JQuery.
thetaiko
@thetaiko: verified that JQuery is the culprit. I've added the JQuery problem area to the OP
dnagirl
@dnagirl - see my answer posted below.
thetaiko
+2  A: 

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.

Sam Bisbee
@Sam Bisbee: All of those things have verified. It appears I'm having a conflict with some JQuery validation that happens prior to submit. It doesn't affect any of the other form fields, just the Submit button.
dnagirl
"and that some other piece of code isn't wiping out $_POST before you get a chance to read it" strikes me as an option, too. Some frameworks (e.g. Zend) blank the superglobals after wrapping them into their own access classes, e.g. if you use Zend's HTTP Request object, you can't check `$_POST`, `$_GET`, et cetera. // Edit: Ninja'ed. Nevermind. :)
pinkgothic
+2  A: 

Change your JQuery to this:

$("form[name='event_form']").validate({
    submitHandler: function(form) {
        $("form[name='event_form'] input[name='event_submit']").click()
    }
}};
thetaiko