1

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?

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Kimberlee Ho
  • 447
  • 1
  • 6
  • 23

1 Answers1

2

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
Yahel
  • 37,023
  • 22
  • 103
  • 153