Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP script with no file writing is deleting file information
    primarykey
    data
    text
    <p>I have a JavaScript update script that updates a chatroom, like this:</p> <pre><code>// Update chat rooms // if(time &gt; lastchatupdatetime + .4){ for(var i in this.chatroomlist){ var chattext = AJAX('server/updateChatRoom.php','id='+i); document.getElementById("S3DChatRoom_" + i).innerHTML = chattext; } lastchatupdatetime = time; } </code></pre> <p>The AJAX function looks like this:</p> <pre><code>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 &amp;&amp; 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; } </code></pre> <p>The PHP script looks like this:</p> <pre><code>$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] . "&lt;br&gt;"; } } } else { echo "Chatroom listed does not exist"; } </code></pre> <p>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 <code>.json</code> 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.</p> <p>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):</p> <pre><code>$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; } </code></pre> <p>This is the JavaScript for posting:</p> <pre><code>var submitChatTextconfirm; document.getElementById("S3DTextInput_"+name).addEventListener("keypress",function(event) { if(event.keyCode == 13){ submitChatTextconfirm = AJAX("server/submitChatText.php", "id=" + name + "&amp;chattext=" + document.getElementById("S3DTextInput_"+name).value + "&amp;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"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload