Hey All, Sorry for reposting(Admins, please delete the other one!). since you guys have been a great help, I was kinda hoping that you could help me once again while having the following question: I am currently trying to work with AJAX by allowing a managerclass in PHP to communicate via an XmlHttpobject with the javascript on the clientside. However, I can send something to the client via JSON, but I cannot read it at the clientside. In fact I am getting the error that the "time" is an undefined index in Session. So I was wondering: what am I doing wrong?
The javascriptcode for Ajax:
<script type="text/javascript">
var sendReq = GetXmlHttpObject();
var receiveReq = GetXmlHttpObject();
var JSONIn = 0;
var JSONOut= 0;
//var mTimer;
//function to retreive xmlHTTp object for AJAX calls (correct)
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
//Gets the new info from the server
function getUpdate() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", "index.php?json="+JSONIn+"&sid=$this->session", true);
receiveReq.onreadystatechange = updateState;
receiveReq.send(null);
}
}
//send a message to the server.
function sendUpdate(JSONstringsend) {
JSONOut=JSONstringsend;
if (sendReq.readyState == 4 || sendReq.readyState == 0) {
sendReq.open("POST", "index.php?json="+JSONstringsend+"&sid=$this->session", true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
alert(JSONstringsend);
sendReq.onreadystatechange = updateCycle;
sendReq.send(JSONstringsend);
}
}
//When data has been send, update the page.
function updateCycle() {
getUpdate();
}
function updateState() {
if (receiveReq.readyState == 4) {
// JSONANSWER gets here (correct):
var JSONtext = sendReq.responseText;
// convert received string to JavaScript object (correct)
alert(JSONtext);
var JSONobject = JSON.parse(JSONtext);
// updates date from the JSONanswer (correct):
document.getElementById("dateview").innerHTML= JSONobject.date;
}
//mTimer = setTimeout('getUpdate();',2000); //Refresh our chat in 2 seconds
}
</script>
The function that actually uses the ajax code:
//datepickerdata
$(document).ready(function(){
$("#datepicker").datepicker({
onSelect: function(dateText){
var JSONObject = {"date": dateText};
var JSONstring = JSON.stringify(JSONObject);
sendUpdate(JSONstring);
},
dateFormat: 'dd-mm-yy'
});
});
</script>
And the PHP code:
private function handleReceivedJSon($json){
$this->jsonLocal=array();
$json=$_POST["json"];
$this->jsonDecoded= json_decode($json, true);
if(isset($this->jsonDecoded["date"])){
$_SESSION["date"]=$this->jsonDecoded["date"];
$this->useddate=$this->jsonDecoded;
}
if(isset($this->jsonDecoded["logout"])){
session_destroy();
exit("logout");
}
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-Type: text/xml; charset=utf-8");
exit($json);
}