Hi i am trying to send Json via Ajax to PHP using Javascript. The data is sent correctly by index.html when I view it on firebug. It shows Json type with the correct data. However, it seems that I am unable to read the JSON on php. I am unable to access it using $_POST. I tried using $_POST['name'] and there is not response. When i try using $_POST, the response is array. Can you please help me?
Here is my javascript code.
<html>
<head>
<script type="text/javascript">
//Create Http request depending on browser
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;}
}
// Function to create
var url = "control.php";
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var data=JSON.stringify({"name":"John", "time":"2pm"});
xmlhttp.send(data);
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
This is my php code
<?php
include_once('JSON.php');
$json = new Services_JSON();
$value = $json->decode($_POST['name']);
echo $value;
?>
Have been working on this for days and I really appreciate any help that you can offer. Thank you!