Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP unserializing a JS serialized string of variables [duplicate]

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.

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=NUMBER+MISSING

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

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?

like image 910
KryptoniteDove Avatar asked Jun 16 '12 15:06

KryptoniteDove


1 Answers

You can use parse_str:

$values = array();
parse_str($_POST['userData'], $values);
echo $values['input1']; //Outputs 'meeting'
like image 67
Emil Vikström Avatar answered Oct 05 '22 23:10

Emil Vikström