18

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?

0

2 Answers 2

57

You can use parse_str:

$values = array();
parse_str($_POST['userData'], $values);
echo $values['input1']; //Outputs 'meeting'
Sign up to request clarification or add additional context in comments.

1 Comment

This is very useful when you are submitting your data in Wordpress through AJAX.
5

I think parse_str is what you need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.