如何从jquery serializearray()中检索$ _POST变量 [英] How to retrieve $_POST variable from jquery serializearray()
问题描述
从jquery serializeArray();
检索$ _POST数据时遇到问题.我尝试for
循环$ _POST以获取数据,但失败了.
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.
这是我的JavaScript代码:
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;
}
在我的PHP代码中:
var_dump($_POST);
结果是这样的:
array(1) {["fields"]=> string(15) "[object Object]"}
那么,任何人都可以教我如何访问$ _POST数据吗?
So, can anyone please teach me how to access the $_POST data?
推荐答案
您无需嵌套序列化的对象;这似乎是导致错误的原因.只需将您的帖子通话设置为:
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");
那应该起作用;您可能还希望从使用serializeArray
更改为使用serialize
.
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
这篇关于如何从jquery serializearray()中检索$ _POST变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!