Kimberlee Ho
Kimberlee Ho

Reputation: 487

How to retrieve $_POST variable from jquery serializearray()

I have a problem in retrieving the $_POST data from jquery serializeArray();. I tried to for loop the $_POST to get the data but failed.

This is my JavaScript code:

    function update_cart(){
    var fields = $(':input').serializeArray();
    console.log(fields);
    var url = "update_cart.php";
    $.post(url, {fields:fields}, function(data) {
         alert(data);
    }, "html");
        return false;
}

In my PHP code :

var_dump($_POST);

The result is this:

array(1) {["fields"]=> string(15) "[object Object]"}

So, can anyone please teach me how to access the $_POST data?

Upvotes: 1

Views: 1451

Answers (1)

Yahel
Yahel

Reputation: 37305

You don't need to nest your serialized object; that seems to be what's causing the error. Just set your post call to:

$.post(url, fields, function(data) {
     alert(data);
     }, "html");

That should work; you might also want to change from using serializeArray to using serialize.

Once this is properly configured, if you have:

<input name="foo" value="bar" />

It can be accessed as:

 $_POST["foo"]; //bar

Upvotes: 2

Related Questions