php - Is there a way to use Ajax to monitor a file, and still update the file using Javascript -


i new web programming, , working on project button pressed on webpage uses javascript write simple value text file through basic php server file. webpage needs have other bells/whistles, , can make of required things work fine, monitor value in text file can show status on webpage. can read status using ajax code, or can send value server file, can't both work in same program.

i figured have pause ajax code monitoring file while writing javascript, no matter how it, can't work. have tried toggling variable , putting ajax code in if statement. have tried executing ajax in while loop using same variable.

an example of code have been trying is:

<script type="text/javascript">  var togglebit; if (togglebit = 'on') {        function savexmldoc()     {         var xmlhttp;         if (window.xmlhttprequest)         {             xmlhttp=new xmlhttprequest();         }         xmlhttp.onreadystatechange=function()         {             if (xmlhttp.readystate==4 && xmlhttp.status==200)             {                 document.getelementbyid("mydiv").innerhtml=xmlhttp.responsetext;             }         }         xmlhttp.open("get","server.php?q=1!",true);         xmlhttp.send();         togglebit = 'off';     } }  if (togglebit = 'off') {        setinterval(function()     {            var ajax = new xmlhttprequest();         ajax.onreadystatechange = function()         {             if (ajax.readystate == 4)             {                 if (ajax.responsetext == "1") {                     document.getelementbyid("p1").innerhtml = "faulted";                     document.getelementbyid("p1").style.fontsize = "xx-large";                     document.getelementbyid("p1").style.color = "red";                 }else if (ajax.responsetext == "0"){                     document.getelementbyid("p1").innerhtml = "okay";                     document.getelementbyid("p1").style.fontsize = "x-large";                     document.getelementbyid("p1").style.color = "green";                 }else{                     document.getelementbyid("p1").innerhtml = "unknown";                     document.getelementbyid("p1").style.fontsize = "x-large";                     document.getelementbyid("p1").style.color = "yellow";                 }             }         };             ajax.open("post", "example.txt", true);          ajax.send();      }, 500); } </script> 

then 1 of many ways have tried set variable stopping ajax code, , run function send value php server file writes text document. tried separating it, , setting disabling bit using onmouseover event, worked better, when run it, status works, , error saying couldn't open file when pressed button.

<p><b>system status: </b><b id ="p1" > </b> </p>  <button type="button" style="color: white" onclick ="togglebit = 'on';savexmldoc()">emergency</br>stop</button> <div id="mydiv"></div> 

php server file code:

<?php $q = $_request["q"]; $myfile = "example.txt"; $fh = fopen($myfile, 'w') or die("can't open file");  $stringdata = "$q\n";  fwrite($fh, $stringdata);  fclose($fh);  ?> 

i'm not looking write code me, if has cleaner way it, or can point me in right direction, appreciated.

thanks in advance.

something should work:

<script type="text/javascript"> var xmlhttp = window.xmlhttprequest ? new xmlhttprequest() : new activexobject('microsoft.xmlhttp');  xmlhttp.onreadystatechange = function() {     if (this.readystate == 4 && this.status == 200)     {         document.getelementbyid('placeholder').innerhtml = this.responsetext;     } }  setinterval(function()     {         // include timestamp prevent caching:         xmlhttp.open('get', 'example.txt?t=' + new date().gettime(), true);         xmlhttp.send();     } , 500);  function write_number(value) {     xmlhttp.open('get', 'server.php?q=' + value, true);     xmlhttp.send();  } </script>  <p>the value is: <span id="placeholder"></span></p> <button onclick="write_number(1)">emergency stop</button> 

you don't need separate ajax objects each function. make global can reused (see code).

there number of errors in original code. it's hard causes not work properly.

also, can't declare functions inside if-statements (or other scopes). piece of code invalid:

if (togglebit = 'on') {        function savexmldoc()     {     } } 

put if-statement inside function instead.


Popular posts from this blog