How to retrieve $_POST variable from jquery serializearray()

余生颓废 提交于 2019-12-24 17:05:19

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8278941/how-to-retrieve-post-variable-from-jquery-serializearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!