submit button not returning value via AJAX/JQuery to self-processing form

我只是一个虾纸丫 提交于 2020-01-06 19:29:14

问题


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;
  });
}

回答1:


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.




回答2:


Try using $_POST['submitButton']




回答3:


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.



来源:https://stackoverflow.com/questions/13424034/submit-button-not-returning-value-via-ajax-jquery-to-self-processing-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!