PHP unserializing a JS serialized string of variables
Solution 1
You can use parse_str:
$values = array();
parse_str($_POST['userData'], $values);
echo $values['input1']; //Outputs 'meeting'
Solution 2
I think parse_str
is what you need.
Related videos on Youtube
KryptoniteDove
++++++++++[>+++++++>++++++++++>+++< <<-]>+++++.>++++++++++++++.+++++++.---------.++++.[>>>>+< <<<-]>>>>[< <+<<+>>>>-]< <<<-----.[>>>>+< <<<-]>>>>[< +<<<+>>>>-]< <<<-.-----.>>.---------------.< <[>>>>+< <<<-]>++.< <-------.>>>>.+++++++.>----.
Updated on July 09, 2022Comments
-
KryptoniteDove almost 2 years
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?
-
Umar Niazi almost 4 yearsThis is very useful when you are submitting your data in Wordpress through AJAX.