-1

I have a form with 17 checkboxes. When one of these changes, JavaScript should submit the values to the server (running PHP).

I want to use JSON, because the checkboxes give two arrays which have to be seperated in PHP

In JS, I create an JSON-String, which I want to submit via POST and read and decode in PHP. The String looks like this atm: [["2015-06-26","2015-06-27"],["2","3","4","5","6","7","8","9","10","11","12","13","14"]] - This is what I want it to be.

This is, what my AJAX-function looks like:

var fullArray = [dateArray, trackArray];
var jsonFullString = JSON.stringify(fullArray);
//jsonFullString == [["a","b","c"],["d","e","f","g"]]
$.ajax({
  type:'POST',
  url:'shownitems.php',
  data: jsonFullString,
  success: function(data){
    //More script. This comment is reached, because
    alert(data);
    // works.
  }
});

When I get it to PHP, and search for $_POST[0] the success function in JS doesn't show anything. When I search for $_POST, I get "Array.." back.

This is, what my PHP looks like (This is my test snippet):

<?php
echo $_POST;
echo ".";
echo $_POST[0];
echo ".";
echo $_POST[0][0];
$array = array();
?>

I am also using jQuery.

8
  • 2
    Use the var_dump() function to print variable content for debugging. There is no $_POST[0], and echo $_POST will print "Array" because it is an array - you'll only ever know what is inside with var_dump($_POST); Commented Jul 13, 2015 at 0:21
  • returns array(0){}... I'm not able to get the content of the arrays ... Even with json_decode(); ... Commented Jul 13, 2015 at 0:47
  • php cannot parse [["2015-06-26","2015-06-27"],["2","3","4","5","6","7","8","9","10","11","12","13","14"]], the post request payload must be strictly key-value pairs. Commented Jul 13, 2015 at 0:49
  • data: 'jsonFullString='+jsonFullString, then look for (and json_decode) $_POST['jsonFullString'] Commented Jul 13, 2015 at 0:50
  • Or just send the object: data: {data: fullArray} without any explicit encodings-decodings anywhere (not sure why people prefer to encode things twice for no real reason) Commented Jul 13, 2015 at 0:50

2 Answers 2

1

In your JS:

/* dateArray and trackArray must be variables with values */
$.ajax({
  method: "POST",
  url: "shownitems.php",
  data: { date: dateArray, track: trackArray }
}).done(function(response) {
  alert(response);
});

In your PHP:

<?php 
  var_dump('Date: '.$_POST['date']);
  var_dump('Track: '.$_POST['track']);
?>
0

You should get your request content instead the $_POST variables.

Something like this would

$json = file_get_contents('php://input');
$obj = json_decode($json); //Will contain your array of two arrays

In case you wanted to get your post variables like you are doing, you could change your AJAX request and use:

$.ajax({
  type:'POST',
  url:'shownitems.php',
  data: {
    data: jsonFullString
  },
  success: function(data){
    //More script. This comment is reached, because
    alert(data);
    // works.
  }
});

And your PHP would be:

$json = $_POST["data"];
$obj = json_decode($json); //Will contain your array of two arrays

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.