I need to pass a javascript object to PHP then decode it as an associative array. I have read many similar questions on this site and googled for a few hrs now. My problem is that I cannot find a SIMPLE example that covers both sides (javascript and PHP). Here is a simplified version of what im trying to do. Posting data as a key/value pair is the only way I can get anything to pass. There is a DIV in the HTML that is used to display the answer. All I want to do is confirm that the data gets past in this code.
All I get is "POST array is loaded with", which to me suggests that the POST array contains data, but im either decoding it wrong or accessing the associative array incorrectly. I have also read the AJAX and POST pages on the JQUERY site but the examples don't get me where I need to be. If I assign strob a simple string value it gets passed perfectly.
$(document).ready(function(){
var obj={};
obj.value1="foo";
obj.value2="bar";
var strob=JSON.stringify(obj);
$.post("byterecord.php",{sendToValue: strob},
function(data){
$('#display').html(data.returnFromValue);
},
"json");
});
byterecord.php
<?php
if (isset($_POST['sendToValue'])){
$aarray=json_decode($_POST['sendToValue'],true);
$strval=$aarray[value1];
}else{
$strval = "nothing.";
}
echo json_encode(array("returnFromValue"=>"POST array is loaded with " .$strval));
?>