submit button not returning value via AJAX/JQuery to self

I have a form which opens in Colorbox and is submitted via Ajax/JQuery to itself. However, it seems as if the data passed is not including the value of the submit button itself. Whether I use multiple submits or just one, there is no data in $_POST['submitButton'], and it doesn't respond to isset() or empty().

The rest of the form posts just fine though. I can echo $_POST['name'] and $_POST['email'], just not $_POST['submitButton']

Here is (a stripped down version of) my form:

<form id="sub-process" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="name" type="text" value="">
<input name="email" type="text" value="">
<input name="submitButton" type="submit" value="Submit">
</form>

And here is the jquery that processes the form to be submitted via AJAX, rather than an HTTP request.

jQuery(function(){
jQuery('.cbox-form').colorbox({maxWidth: '75%', onComplete: function(){
cbox_submit();
}});
});

function cbox_submit()
{
jQuery("#sub-process").submit(function(){
jQuery.post(
  jQuery(this).attr('action'),
  jQuery(this).serialize(),
  function(data){
    jQuery().colorbox({html: data, onComplete: function(){
      cbox_submit();
    }});
  }
);
return false;
  });
}

I know this is an old question but It looks like people do not understand that @itachi has the correct answer.

The serialize method will NEVER return a value from the submit button. It does not return the submit button in the post. You will do best to just use a hidden form field and adding a click event to the button.


尝试使用$_POST['submitButton']


jQuery's serialize() function is kind of quirky and particular. I find it to be not so useful for many scenarios. You may want to try the serializeObject plugin. I've found it to work in many cases when serialize() does not work for me.

链接地址: http://www.djcxy.com/p/69836.html

上一篇: JQuery event.preventdefault和php

下一篇: 提交按钮不通过AJAX / JQuery返回值给自己