1

I have a working program but I just learned about the 'serialize' option in JQuery and PHP that I think can clean up my code. Problem is that after reading a few great posts on here im still not getting it to work. Can someone take a look if ya dont mind?

Here is what I am sending to the PHP page (serialized form data):

contForm is the name of my form dispAdd is the callback which Im not worried about getting to just yet

var formData = $('#contForm').serialize();
$.post('functs.php',formData,dispAdd);
 function dispAdd(status) {
 if (status=='added') {
 $('#main').html('<div>Your have been added to the mailing list</div>');
 } else {
 if ($('#fail').length==0) {
 $('#main'.prepend('<div>This email address is already subscribed to our mailing list</div>');
 }
 }
 }

I know it is sending the string over but I am having a problem OUTPUTTING the results. I plan on using the output in a PDO to access a Database. I was trying parse_str but its just not working. Here is how I was formatting:

function addMember() 
{
 $params = array();
 parse_str($_POST, $params);

Thats as far as I can get. I am trying to figure out how to access the new variables (the serialized form data). Any tips?

asked Mar 12, 2013 at 4:51
3
  • 1
    AFAICT the value of $_POST should already contain your data as an array. Commented Mar 12, 2013 at 4:53
  • Did you try to unserialize it in your PHP code? (php.net/manual/en/function.unserialize.php) Commented Mar 12, 2013 at 4:54
  • Why would one use serialize then if POST will carry all the data anyway? Excuse my ignorance but I think I may have misunderstood the need for serialize as it seems its not even needed here Commented Mar 12, 2013 at 5:04

1 Answer 1

1

Your request gets sent via AJAX as application/x-www-form-urlencoded and is decoded into $_POST.

If you have an <input name="test" /> inside your form, after posting it would become available as:

$_POST['test']
answered Mar 12, 2013 at 4:55
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for replying. So can you help me figure out the format for my syntax? The name 'formData' is what i used to serialize the form. should it be parse_str('formData', $_POST) then?
@RobertMailloux No, just $_POST['an-input-name'] should just work.
@RobertMailloux jQuery.serialize() takes all the input elements in your form and creates a string that's submitted to PHP; this in turn is decoded into $_POST.
Thanks Jack, but for the heck of it, I removed the serialize from my code, and in fact, just sent the form without any jquery at all and it still sent the data to the POST. Is serialize doing something I am missing because it doesnt seems to be needed.
@Robert jQuery does a few things for you behind the scenes when you do that, which is why it works without an explicit serialize() calls.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.