我如何对 jQuery 序列化的表单进行 PHP 反序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792603/
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
How do I PHP-unserialize a jQuery-serialized form?
提问by Gene Bean
Using $('#form').serialize(), I was able to send this over to a PHP page. Now how do I unserialize it in PHP? It was serialized in jQuery.
使用$('#form').serialize(),我能够将其发送到 PHP 页面。现在我如何在 PHP 中反序列化它?它是在 jQuery 中序列化的。
采纳答案by Chris Gutierrez
You shouldn't have to unserialize anything in PHP from the jquery serializemethod. If you serialize the data, it should be sent to PHP as query parameters if you are using a GET method ajax request or post vars if you are using a POST ajax request. So in PHP, you would access values like $_POST["varname"]or $_GET["varname"]depending on the request type.
您不必从 jqueryserialize方法中对 PHP 中的任何内容进行反序列化。如果您序列化数据,如果您使用 GET 方法 ajax 请求,则应将其作为查询参数发送到 PHP;如果您使用 POST ajax 请求,则应将其作为查询参数发送到 PHP。因此,在 PHP 中,您可以访问类似$_POST["varname"]或$_GET["varname"]取决于请求类型的值。
The serializemethod just takes the form elements and puts them in string form. "varname=val&var2=val2"
该serialize方法仅采用表单元素并将它们置于字符串形式中。"varname=val&var2=val2"
回答by Chris Allen Lane
Provided that your server is receiving a string that looks something like this (which it should if you're using jQuery serialize()):
假设您的服务器正在接收一个看起来像这样的字符串(如果您使用的是 jQuery 应该是这样serialize()):
"param1=someVal¶m2=someOtherVal"
...something like this is probably all you need:
......这样的事情可能就是你所需要的:
$params = array();
parse_str($_GET, $params);
$paramsshould 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 Aridane
// jQuery Post
// jQuery 帖子
var arraydata = $('.selector').serialize();
// jquery.post serialized var - TO - PHP Array format
// jquery.post 序列化 var - TO - PHP 数组格式
parse_str($_POST[arraydata], $searcharray);
print_r($searcharray); // Only for print array
// You get any same of that
// 你得到任何相同的
Array (
[A] => 1
[B] => 2
[C] => 3
[D] => 4
[E] => 5
[F] => 6
[G] => 7
[H] => 8
)
回答by danidacar
parse_str($_POST['whatever'], $searcharray);
回答by mghhgm
In HTMLpage:
在HTML页面中:
<script>
function insert_tag()
{
$.ajax({
url: "aaa.php",
type: "POST",
data: {
ssd: "yes",
data: $("#form_insert").serialize()
},
dataType: "JSON",
success: function (jsonStr) {
$("#result1").html(jsonStr['back_message']);
}
});
}
</script>
<form id="form_insert">
<input type="text" name="f1" value="a"/>
<input type="text" name="f2" value="b"/>
<input type="text" name="f3" value="c"/>
<input type="text" name="f4" value="d"/>
<div onclick="insert_tag();"><b>OK</b></div>
<div id="result1">...</div>
</form>
on PHPpage:
在PHP页面上:
<?php
if(isset($_POST['data']))
{
parse_str($_POST['data'], $searcharray);
$data = array(
"back_message" => $searcharray['f1']
);
echo json_encode($data);
}
?>
on this php code, return f1field.
在此 php 代码中,返回f1字段。
回答by Tomasz Majerski
Modified Murtaza Hussainanswer:
修改后的穆尔塔扎侯赛因回答:
function unserializeForm($str) {
$strArray = explode("&", $str);
foreach($strArray as $item) {
$array = explode("=", $item);
$returndata[] = $array;
}
return $returndata;
}
回答by Murtaza Khursheed Hussain
Why don't use associative array, so you can use it easily
为什么不使用关联数组,这样你就可以轻松使用它
function unserializeForm($str) {
$returndata = array();
$strArray = explode("&", $str);
$i = 0;
foreach ($strArray as $item) {
$array = explode("=", $item);
$returndata[$array[0]] = $array[1];
}
return $returndata;
}
Regards
问候
回答by Omid Akhavan
Simply do this
只需这样做
$get = explode('&', $_POST['seri']); // explode with and
foreach ($get as $key => $value) {
$need[substr($value, 0 , strpos($value, '='))] = substr(
$value,
strpos( $value, '=' ) + 1
);
}
// access your query param name=ddd&email=aaaaa&username=wwwww&password=wwww&password=eeee
var_dump($need['name']);
回答by Varga Zoli
Use:
用:
$( '#form' ).serializeArray();
Php get array, dont need unserialize ;)
Php 获取数组,不需要反序列化 ;)
回答by jbz
This is in reply to user1256561. Thanks for your idea.. however i have not taken care of the url decode stuff mentioned in step3.
这是对 user1256561 的回复。感谢您的想法.. 但是我没有处理步骤 3 中提到的 url 解码内容。
so here is the php code that will decode the serialized form data, if anyone else needs it. By the way, use this code at your own discretion.
所以这里是 php 代码,它将解码序列化的表单数据,如果其他人需要的话。顺便说一句,请自行决定使用此代码。
function xyz($strfromAjaxPOST)
{
$array = "";
$returndata = "";
$strArray = explode("&", $strfromPOST);
$i = 0;
foreach ($strArray as $str)
{
$array = explode("=", $str);
$returndata[$i] = $array[0];
$i = $i + 1;
$returndata[$i] = $array[1];
$i = $i + 1;
}
print_r($returndata);
}
The url post data input will be like: attribute1=value1&attribute2=value2&attribute3=value3 and so on
url post 数据输入将类似于:attribute1=value1&attribute2=value2&attribute3=value3 等等
Output of above code will still be in an array and you can modify it to get it assigned to any variable you want and it depends on how you want to use this data further.
上述代码的输出仍将在一个数组中,您可以修改它以将其分配给您想要的任何变量,这取决于您希望如何进一步使用这些数据。
Array
(
[0] => attribute1
[1] => value1
[2] => attribute2
[3] => value2
[4] => attribute3
[5] => value3
)

