Jony Kale
Jony Kale

Reputation: 189

PHP unserialize jQuery FORM post

<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!

Upvotes: 1

Views: 8366

Answers (4)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

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

Upvotes: 0

Musa
Musa

Reputation: 97672

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

Upvotes: 8

monkeyinsight
monkeyinsight

Reputation: 4859

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

Upvotes: 2

JPR
JPR

Reputation: 869

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

Upvotes: -1

Related Questions