I just want receive the array and print it which is sent by jquery.post()
my HTML-JS
<input type="submit" id="myButton" />
<script type="text/javascript">
var arr = new Array();
arr[0] = "Ding";
arr[1] = "Dong";
arr[2] = "Bong";
var json_string= JSON.stringify(arr); // convert it into a json string.
//and sending the data to server. temp.php.
$("#myButton").click(function(){
$.post('temp.php' , {json : json_string },function(data){
location.href = "temp.php";
});
});
</script>
I have checked by alert() that data sending is successful. After button click page is also redirecting to temp.php
My php page temp.php
<?php
$json_string = $_POST['json']; //geting the post request. //this is line 3
$array_items = json_decode($json_string); //converting json string to a php array.
echo $array_items[0]; //just to print 1st element of array
?>
It should print Ding. But I am getting error
Undefined index: json in C:\wamp\www\phpJSarray\temp.php on line 3
What is my mistake in temp.php page ? Please help me out.
print $json_string;and if that's good, on line 6 tryprint_r($array_items);to see if you have an array with expected values.$json_string = $_POST['json'];The possibilities of an error are rather broad without seeing full codes. Is there not a named element such asname="json"?