0

Ok, so here is my JS/jQuery code, my rate.php file simply has a print_r($_POST) in it. The problem is, the $_POST is accepting rated as the string "Array", rather than the actual array as I have defined it. How do I correct this code so PHP will recognize the JSON input as a proper array, rather than a string?

var rated = {"key" : key , "value" : value};

$.ajax({
  type: "POST",
  url: $(location).attr('protocol') + "//" + $(location).attr('hostname') + "/ajax/rate.php",
  data: {
    "rated" : rated
  },
  success: function(data) {
    alert(data);
  }
});

This is the output message I'm getting:

Array ( [rated] => Array )
Fatal error: Only variables can be passed by reference in .../ajax/rate.php on line X


EDIT: There are actually more variables that rated, but none of them are arrays (thus there isn't an issue with them), so I cut them out of the code above for brevity sake.

2 Answers 2

2

When passing JSON data to your php script through ajax I would recommend string encoding the JSON data and then parsing it on the server side.

var rated = {"key" : key , "value" : value};
var rated_encoded = JSON.stringify(rated);

$.ajax({
  type: "POST",
  url: $(location).attr('protocol') + "//" + $(location).attr('hostname') +     "/ajax/rate.php",
  data: {
    "rated" : rated_encoded
  },
  success: function(data) {
    alert(data);
  }
});

Then you should be able to access the POST variable in your PHP script using $_POST as with any other scalar value. Once you have the JSON string 'rating_encoded' on the server-side, parse it to an associative array using PHP's json_decode().

if(isset($_POST["rated"])){
    $rated_json = $_POST["rated"];
    $JSONArray  = json_decode($rated_json, true); //returns null if not decoded
    //Values can now be accessed like standard PHP array
    if($JSONArray !== null){ 
        $key = $JSONArray["key"];
        $value = $JSONArray["value"];
    }
}    

I've found that this method is very effective for transferring javascript object data to the server and vice versa (using PHP's json_encode() to translate PHP arrays into valid javascript objects)

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

Comments

0

It is a proper array, just not what you expect it to be. What you likely want can be achieved by simply passing rated to the data parameter as is. I.e.

var rated = {"key" : key , "value" : value};

$.ajax({
  type: "POST",
  url: $(location).attr('protocol') + "//" + $(location).attr('hostname') + "/ajax/rate.php",
  data: rated,
  success: function(data) {
    alert(data);
  }
});

2 Comments

There are actually more variables that rated, but none of them are arrays (thus there isn't an issue with them), so I cut them out of the code above for brevity sake.
@Matt, then what is the request data are you expecting? Because right not you are passing parameter rated with value {"key" : key, "value" : value} and HTTP doesn't support parameter nesting.

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.