0

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; }); } 
4
  • 2
    serialize won't include submit button value because form was not submited by submit button. Only "successful controls" are serialized to the string. Commented Nov 16, 2012 at 20:50
  • Thats frustrating. I'm considering working around it with a hidden value, but what JS would I use to change the value of that hidden button depending on which button is clicked and submit the form? Commented Nov 16, 2012 at 20:57
  • Put a click handler on the submit button, and have it add an appropriate property to the object that it submits. Commented Nov 16, 2012 at 21:00
  • Looks like I'm going to change my extra buttons to checkboxes, and let "checked" trigger the required actions, that way I can maintain one submit, and use a hidden value to check isset($_POST['submit']) Commented Nov 16, 2012 at 21:00

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using $_POST['submitButton']

6 Comments

Maybe I'm missing something, but the OP stated there is no data in $_POST['submitButton']
I just edited when i realized that i had written $_POST['submit'] at first, instead of $_POST['submitButton']. I've tried both, to no avail.
try var_dump($_POST) to view all values stored in $_POST array
Update: Same issue when I use JS to submit the form - onClick="document.forms["sub-process"].submit();"
var_dump($_POST) yields: array(2) { ["name"]=> string(4) "test" ["email"]=> string(13) "[email protected]" }
|
0

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.

1 Comment

it is peculiar about it because that's the w3 standard in the definition of successful controls.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.