title: Update text file on button click
body: <p>I want to make a website where there is a button, and when you click it make it change some text (saying either true/false). The text must change when the user clicks the button, but not just for the one user, for every user that's on the site.</p>
  
  <p>The way I'm trying to do it is I have a text file, and so far all I've got is some text on the page that every 500 milliseconds is refreshing to display whatever is in the text file.</p>
  
  <p>So now all I need to do is update the text file when the button is clicked. What's the best way I could do this? (I want to be able to press the button on the computer hosting the site OR another computer accessing the site.)</p>
  
  <p>Thanks,
  Fjpackard.</p>
  
  <hr>
  
  <p><em>Update</em></p>
  
  <p>index.php:</p>
  
  <pre><code>&lt;script src="http://code.jquery.com/jquery-1.10.1.min.js"&gt;&lt;/script&gt;
  
  &lt;body&gt;
  
  &lt;div id="theDiv"&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  
  &lt;script&gt;
  
  $("document").ready(function(){
      $("button").remove();
      setInterval(function(){
           $("#theDiv").load("file.txt");
      },500);
      var deviceAgent = navigator.userAgent.toLowerCase();
      var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
      if (agentID) {
  
          // mobile code here
  
      }
      else {
          $("body").prepend("&lt;button&gt;Toggle&lt;/button&gt;");
          $("#theDiv").css("visibility","none");
  
          $("button").click(function(){
              myClick();
          });
      }
  });
  
  function myClick() {
                  var url = ''; //put the url for the php
                  value = $.post("", {}).done(
                      function(data) {
                          $('#theDiv').html(data);
                      }
                  );
              }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>script.php:</p>
  
  <pre><code>&lt;?php
      if ($_SERVER['REQUEST_METHOD'] === 'POST')
      {
          $file = 'file.txt';
  
          $previous = file_get_contents($file);
          if ($previous === 'true')
          {
              file_put_contents($file, 'false');
              echo 'false'; //this is what we return to the client
          }
          else
          {
              file_put_contents($file, 'true');
              echo 'true'; //this is what we return to the client
          }
          exit();
      }
  ?&gt;
  </code></pre>