1

I'm coding a form right now using jQuery .post and the file responsible for processing has the code:

print_r($_POST);

Which returns the following dynamic output:

Array ( [data] => capacity=50-1000+people&bookingdate=17%2F04%2F2012&grade=1+star )

I am trying to split up this array into three variables namely capacity, booking date and grade but don't really know how to. Any idea how? I've tried using echo $_POST["capacity"]; but it doesn't work.

Thanks in advance!

Edit

This is the jQuery I'm using:

<script type="text/javascript">
$(document).ready(function() {
    $("#postData").click(function() {
        $("#last-step").hide(600);


       $.post('resources/process2.php', { data: $("#task5_booking").serialize() }, function(data) {
            $("#result").html(data);
      });


        return false;
    });
});
</script>

which is working with the following form:

http://jsfiddle.net/xSkgH/93/

4
  • 2
    Looks like you're double URL encoding the data. While it's easy to answer your question literally, the real problem seems to be in how you're POSTing the data from the jQuery side. Commented Apr 17, 2012 at 11:37
  • i guess it will be muck better to encode your data into json in js and then decode in the server side Commented Apr 17, 2012 at 11:38
  • showing the jQuery side will a good idea. Commented Apr 17, 2012 at 11:39
  • I've updated the question to show the jQuery as well. Commented Apr 17, 2012 at 11:42

3 Answers 3

4

I think you should change this line:

$.post('resources/process2.php', { data: $("#task5_booking").serialize() }, function(data) {

To

$.post('resources/process2.php', $("#task5_booking").serialize(), function(data) {

Notice that I changed the second parameter from an object literal to a (url-encoded) string. This posts each variable in your form as a separate variable (as if it were posted directly). On the server side, each variable should be available separately inside $_POST array.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a go with parse_str()

Something like:

parse_str($_POST['data'] , $output);

$capacity = $output['capacity'];
$bookingdate = $output['bookingdate'];
$grade = $output['grade'];

Comments

1

You have to use explode for this.

$data = array();  // A array to store the extracted value
$temp = explode("&", $data); // First of all explode by "&" to get the each piece
foreach($temp as $tval) {
   $t = explode('=', $tval); // Next explode by "=" to get the index and value
   $data[$t[0]] = $t[1];  //Add them to the array
}

Another alternative is to use parse_str():

$data = array();
parse_str($_POST['data'], $data);

After this all the values will be mapped to the $data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.