2

Why can't I access my array through $_POST in PHP? I'm trying to use the jQuery $.post method. Here is the corrected code with your suggestions:

My javascript:

<script type="text/javascript">
var selectedValues;
var serializedValues;
$("td").click(function() {
$(this).toggleClass('selectedBox');

// map text of tds to selectedValues
selectedValues = $.map($("td.selectedBox"), function(obj) {
        return $(obj).text();

});

serializedValues = JSON.stringify(selectedValues);

// $.post('/url/to/page', {'someKeyName': variableName}); //exemple
$.post('handler.php', 
      {'serializedValues' : serializedValues}, 
      function(data) {
        //debug 
     }
);

});

</script>

My php:

<?php
if(isset($_POST['serializedValues'])) {

            var_dump($_POST['serializedValues']);
            $originalValues = json_decode($_POST['serializedValues'], 1);
            print_r($originalValues);

        }


?>
6
  • Have you looked at what you're getting at the server using var_dump($_POST)? Commented Jun 17, 2012 at 13:26
  • 1
    Would be nice to see the content of serializedValues Commented Jun 17, 2012 at 13:26
  • have tried var_dump($_POST), empty array, gonna work on this i guess Commented Jun 17, 2012 at 13:38
  • it is strange because i have performed some debug with alert() and each i click on the items i want, an array correctly generated with the correct value. @Dr.Molle, the content of serializedValues is just a serialization of the selectedValues, and it is generated properly too Commented Jun 17, 2012 at 13:39
  • 1
    Side note: most browsers have kind of developer tools panel. There you can inspect what is actually send when you make an AJAX request. Commented Jun 17, 2012 at 14:04

2 Answers 2

4

You should serialize your array into json string:

serializedValues = JSON.stringify(selectedValues)

And pass it to php. And then decode with json_decode:

$originalValues = json_decode($_POST['serializedValues'], 1);

http://php.net/manual/ru/function.json-decode.php

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

20 Comments

thanks, i've read tons of stuff about json, but i was a bit afraid. I will try, seems like everyone love this now ^^
well, i've tried this. But the problem still remain the same, when i issue an isset($_POST['serializedValues']) in php, nothing is found.
what does var_dump($_POST['serializedValues']) say?
Well i figured out why it was strange, the output of console.log(selectedValues) was wrong from me. Big mistakes from me on the other version of the file, so it shows : ReferenceError: selectedValues is not defined
It's a scope problem, i need to declare selectedValues outside of the click() function. If I do that, console.log(selectedValues) output properly now, and console.log(JSON.stringify(selectedValues) output properly aswell
|
2

On a side note; your javascript could be refactored into something a bit more simple

$("td").click(function() {
    $(this).toggleClass('selectedBox');

    // map text of tds to selectedValues
    var selectedValues = $.map($("td.selectedBox"), function(obj) {
            return $(obj).text();
    });

    // $.post('/url/to/page', {'someKeyName': variableName}); //exemple
    $.post('handler.php', 
          {'serializedValues' : JSON.stringify(serializedValues)}, 
          function(data) {
            //debug 
         }
    );
});

4 Comments

i really like the way you select td, thanks for the lesson :)
ive tried it but seems like your map function doesn't push the values of td properly. when i alert(selectedValues), it just shows me ",".
ah, had to change $(this) by $(obj), pretty obvious
oh, sorry, edited that. also I removed the .filter(), since there's no benefit in using it in this case instead of a normal selector

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.