Javascript PHP反序列化一个JS序列化的变量字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11064529/
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
PHP unserializing a JS serialized string of variables
提问by KryptoniteDove
I have the following code when a user clicks in the 'generate' element the data within the form 'serializeData' is serialized in js. This string is passed to the loadTemplate function which in turn POST's that string with other variables to a php script for processing.
当用户单击“生成”元素时,我有以下代码,表单“serializeData”中的数据在 js 中被序列化。该字符串被传递给 loadTemplate 函数,该函数又将该字符串与其他变量一起发送到 php 脚本进行处理。
What I'm looking for is a way to unserialize the js string in PHP or best practice for getting at the data, here is an example output of the serilized data as seen in PHP as a string: -
我正在寻找的是一种在 PHP 中反序列化 js 字符串的方法或获取数据的最佳实践,这是在 PHP 中作为字符串显示的序列化数据的示例输出:-
input1=meeting&input2=select+date&input3=enter+text&input4=NUMBER+MISSING
input1=会议&input2=select+date&input3=enter+text&input4=NUMBER+MISSING
The serialized form data is passed to PHP in the loadTemplate function in the userData variable.
序列化的表单数据在 userData 变量中的 loadTemplate 函数中传递给 PHP。
Functions: -
职能: -
$("#generate").click(function () {
if (eCheck == true) {
var templateData = $("#serializeData").serialize();
var templateID = $("#serializeData").attr("name");
loadTemplate(this, templateID, 3, templateData)
}
return false;
});
function loadTemplate(obj, cat, call, userData) {
userData = typeof userData !== "undefined" ? userData : null; // Set userData to null if undefined.
var onSuccess = "#right";
if (call == 1) {
onSuccess = "#left";
switchButton(obj);
$("#content").hide();
$("#right-content").text("");
}
$("#loading").show();
$.ajax({
type: "POST",
url: "./scripts/load.php",
data: { id : cat, call: call, userData: userData },
cache: false,
success: function(html){
$(onSuccess + "-content").html(html);
if (onSuccess == "#left") {
$("#content").fadeIn(500);
}
$("#loading").fadeOut(500);
resizeAll();
}
});
}
Any thoughts?
有什么想法吗?
回答by Emil Vikstr?m
回答by Agares
I think parse_str
is what you need.
我认为parse_str
这就是你所需要的。