在使用 AJAX 调用的 PHP 文件中检索序列化数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9430918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving serialize data in a PHP file called using AJAX
提问by MAK
Form sending AJAX code:
表单发送 AJAX 代码:
var str = $("form").serialize();
alert(str);
// var uns=@unserialize(str);
//alert(uns);
$.ajax({
type: "POST",
url: "update.php",
data: "box1="+str,
success: function(value)
{
$("#data").html(value);
}
HTML Form:
HTML 表单:
<form>
<input type=checkbox name=box[] value='1'/><input type=checkbox name=box[] value='2'/>
</form>
In my PHP:
在我的 PHP 中:
$box=$_POST['box1'];
How can I access each of the box variable values in PHP side?
如何访问 PHP 端的每个框变量值?
回答by rasmusx
Your js should be like this:
你的js应该是这样的:
var str = $("form").serializeArray();
$.ajax({
type: "POST",
url: "update.php",
data: str,
success: function(value) {
$("#data").html(value);
}
});
With php you should loop your result array.
使用 php 你应该循环你的结果数组。
$box = $_POST['box'];
foreach ($box as $x) {
echo $x;
}
Edit:You have to use serializeArray function in jQuery. Then it will work with this code.
编辑:您必须在 jQuery 中使用 serializeArray 函数。然后它将使用此代码。
回答by Mouna Cheikhna
your data in php will contain a string like this
您在 php 中的数据将包含这样的字符串
field1=value1&field2=value2&....
field1=value1&field2=value2&....
so you can get your value1 using $_POST['field1]
, value2 with $_POST['field2']
所以你可以使用 value1 $_POST['field1]
, value2 与$_POST['field2']
回答by Vikram Singh Katewa
$data = array();
foreach(explode('&', $_POST[data]) as $value)
{
$value1 = explode('=', $value);
$data[$value1[0]] = validateInput($value1[1]);
}
var_dump($data['box']);
回答by user3230699
Provided that your server is receiving a string that looks something like this
假设您的服务器正在接收一个看起来像这样的字符串
$("form").serialize();
"param1=someVal¶m2=someOtherVal"
...something like this is probably all you need:
......这样的事情可能就是你所需要的:
$params = array();
parse_str($_GET, $params);
$params should then be an array modeled how you would expect. Note this works also with HTML arrays.
$params 应该是一个按照您期望的方式建模的数组。请注意,这也适用于 HTML 数组。
See the following for more information: http://www.php.net/manual/en/function.parse-str.php
有关更多信息,请参阅以下内容:http: //www.php.net/manual/en/function.parse-str.php
Hope that's helpful. Good luck!
希望这有帮助。祝你好运!
回答by Sujit Verma
your JS should be like this -
你的JS应该是这样的——
var str = $( "form" ).serializeArray();
var postData = new FormData();
$.each(str, function(i, val) {
postData.append(val.name, val.value);
});
$.ajax({
type: "POST",
data: postData,
url: action,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert(data);
}
});
Now do this in your php script -
现在在您的 php 脚本中执行此操作 -
print_r($_POST);
you will get all form data in alert box.
您将在警报框中获得所有表单数据。
回答by Jagadesh
values=$("#edituser_form").serialize();//alert(values);
$.ajax({
url: 'ajax/ajax_call.php',
type: 'POST',
dataType:"json",
data: values,
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
回答by Alasjo
Change
改变
data: "box1="+str,
into
进入
data: str,
serialize()
will produce a string like: input1=value1&input2=value2
. So in your php you can access each value with, for instance $value1 = $_PHP['input1'];
serialize()
将产生一个字符串,如:input1=value1&input2=value2
。因此,在您的 php 中,您可以访问每个值,例如$value1 = $_PHP['input1'];
回答by Lance
$box=$_POST['box']; and $box is an array.
$box=$_POST['box']; $box 是一个数组。