Of course, a stupid mistake, but I do not see it

I have a form (very 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 an EventForm class to handle the form. The main method, process () is as follows:

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 submitting a form, this happens: $form->process('event_submit');For some reason, it isset($_POST['event_submit'])always evaluates to FALSE. Can anyone understand why?

ETA: after working with sentences, it seems that JQuery.validate () has an unexpected effect on the presentation of the form. All fields are sent, except for the "Submit" button. It looks like this is a jQuery bug:

$("form[name='event_form']").validate({
    submitHandler: function(form) {
        form.submit();
    }
}};

Any thoughts on how to send the value of the submit button?

+3
source share
3 answers

JQuery :

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

print_r $_POST , - , .

 print_r($_POST);
+2

I broke your code into an 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" are displayed as expected. Make sure your class is created correctly, that you are actually passing event_submit to it, and that some other piece of code does not destroy $ _POST before you can read it.

+2
source

Source: https://habr.com/ru/post/1742120/


All Articles