PHP反序列化JS序列化变量字符串 [英] PHP unserializing a JS serialized string of variables

查看:308
本文介绍了PHP反序列化JS序列化变量字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击'generate'元素时,我有以下代码,'serializeData'形式的数据在js中被序列化。这个字符串被传递给loadTemplate函数,而loadTemplate函数又将带有其他变量的字符串POST到php脚本进行处理。

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.

我正在寻找的是一种反序列化的方法PHP中的js字符串或获取数据的最佳实践,以下是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: -

input1 = meeting& input2 = select + date& input3 = enter + text& input4 = NUM​​BER + MISSING

input1=meeting&input2=select+date&input3=enter+text&input4=NUMBER+MISSING

序列化表单数据在userData变量的loadTemplate函数中传递给PHP。

The serialized form data is passed to PHP in the loadTemplate function in the userData variable.

函数: -

$("#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();
    }
});

}

有什么想法吗?

推荐答案

您可以使用 parse_str

$values = array();
parse_str($_POST['userData'], $values);
echo $values['input1']; //Outputs 'meeting'

这篇关于PHP反序列化JS序列化变量字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆