1
<form action="index.php" method="POST" id="form">
<input type="text" name="guest" id="guest_name" class="textbox"/><br />
<textarea name="textarea" id="text" class="textarea"></textarea/><br />
<input type="submit" id="submit" class="submit"/><br />
</form>

Jquery

$.post("events.php?action=send", { data :  $("#form").serialize() }, function(data, error) { }

Tested if post DATA has the data in it:

echo var_dump($_POST['data']);

I get this:

name=blabla&comment=blabla1

And then when I do

echo $_POST['guest'];

Nothing comes up, it's a NULL.

Question:

What have I done wrong? Why doesn't the POST guest get filled? if it's in DATA, and form's method is POST too.

Thanks!

1
  • It's because you are serializing the form in to the data field in your JQuery that you are getting it like that in PHP. Try getting rid of the data : part in the post function so the data will be past to the $_POST array as expected. Commented Apr 14, 2013 at 19:12

4 Answers 4

8

Pass the serialized string as the data parameter to $.post not an object whose data parameter is the serialized string

$.post("events.php?action=send", $("#form").serialize() , function(data, error) { }

Now you'll be able to access $_POST['guest'] etc

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

Comments

2
<?php
parse_str($_POST['data'], $data);
print_r($data);

Comments

0

You are doing it in the wrong way. Try like this

$.post(
        "events.php?action=send", 
        $("#form").serialize() , 
        function(data, error) {}
);

On php end access $_POST array like this

echo $_POST['guest'];
echo $_POST['textarea'];

Remove the data key you don't need it.$("#form").serialize() will make a query string in post which you can easily access as you get on usuall form submission.

References

JQuery .serialize

3 Comments

Object literals must have key/value pairs. Line 3 of your code is not an improvement.
@Quentin before downvoting and commenting see the result. i am not going to remove my answers anyways because i have worked using like this for months.
what a shame the same thing posted by @Musa gets vote up and i get vote down without any reason.
-1

From what you've written it looks like $_POST['data']['guest'] would have what you're looking for in it.

Comments

Your Answer

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.