I have a JavaScript update script that updates a chatroom, like this:
// Update chat rooms //
if(time > lastchatupdatetime + .4){
for(var i in this.chatroomlist){
var chattext = AJAX('server/updateChatRoom.php','id='+i);
document.getElementById("S3DChatRoom_" + i).innerHTML = chattext;
}
lastchatupdatetime = time;
}
The AJAX function looks like this:
var AJAX = function (page, send, callback){
if(typeof callback == 'undefined') callback = function() {};
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else { // for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function (){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
chatresponse = xmlhttp.responseText;
callback();
}
}
xmlhttp.open("POST",page,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(send);
return chatresponse;
}
The PHP script looks like this:
$chatroomid = isset($_POST['id'])?$_POST['id']:0;
$chatroomdir = "../chatrooms/";
$chatroomurl = $chatroomdir . 'chatroom_' . $chatroomid . ".json";
if(file_exists($chatroomurl)){
$chattext = json_decode(file_get_contents($chatroomurl),TRUE);
if($chattext == TRUE){
foreach($chattext as $i){
echo $i[0] . ": " . $i[1] . "<br>";
}
}
} else {
echo "Chatroom listed does not exist";
}
After I submit a new chattext, the text gets written and on first update it displays, and then on next update it gets deleted from the .json
file that holds the chat contents. This makes absolutely no sense to me, and I've written similar chat applications without this problem even remotely existing. This happens on WebMatrix's IIS-Express PHP and on WAMP's PHP.
Here's the chat submit PHP script (with some debugging code left in it -- the debugging code works fine and all the information is correctly written):
$chatroomid = $_POST['id'];
$chatroomtext = $_POST['chattext'];
$clientid = $_POST['clientid'];
$chatroomdir = "../chatrooms/";
$chatroomurl = $chatroomdir . "chatroom_" . $chatroomid . ".json";
$currentchattext = file_get_contents($chatroomurl);
$currentchattext = json_decode($currentchattext, TRUE);
$currentchattext[] = array($clientid,$chatroomtext);
file_put_contents("debug2",json_encode($currentchattext));
$success = file_put_contents($chatroomurl, json_encode($currentchattext));
file_put_contents("debug",json_encode($currentchattext), FILE_APPEND);
if($success == FALSE){
echo 0;
} else {
echo 1;
}
This is the JavaScript for posting:
var submitChatTextconfirm;
document.getElementById("S3DTextInput_"+name).addEventListener("keypress",function(event) {
if(event.keyCode == 13){
submitChatTextconfirm = AJAX("server/submitChatText.php", "id=" + name + "&chattext=" + document.getElementById("S3DTextInput_"+name).value + "&clientid=" + this.clientid);
console.log(document.getElementById("S3DTextInput_"+name).value);
console.log(submitChatTextconfirm);
}
},true);
if(submitChatTextconfirm == 1){
console.log("Chat sent");
} else {
console.log("ERROR: Chat text failed to send");
}