1

So I have a code, where a number is retrieved from a text file and is displayed through HTML.

The thing is that it only retrieves the number when I refresh the page. If I change the number on other page, it doesn't change on the other page.

I've seen similar questions before, most of them said to use AJAX, but I don't understand much about AJAX and I only need it for this bit of code, it's not like I'm going to constantly use it. I would like to know if there is any other besides AJAX and if there is only AJAX, please present examples of code.

PHP code:

<?php
    $file = "num.txt"; //Path to your *.txt file
    $contents = file($file);
    $num = implode($contents);
?>

JavaScript code. Basically it gets the PHP value and outputs to the html.

document.getElementById("num").innerHTML = '<?php echo $num; ?>';

Keep in mind, I don't want to refresh the page. Just the variable.


EDIT: PHP code - Getting an error - Notice: Undefined index: keynum in C:\xampp\htdocs\num.php on line 3

Here is the code of the PHP

<?php
    $keynum = $_POST['keynum'];
    $post = "INSERT INTO post (keynum) VALUES ($keynum)";
    $fileLocation = getenv("DOCUMENT_ROOT") . "/num.txt";
    $file = fopen($fileLocation,"w");
    $content = $keynum;
    fwrite($file,$content);
    fclose($file);
    echo 'Response';
    die();
 ?>
user7637745
  • 965
  • 2
  • 14
  • 27
  • 4
    Didn't you [already ask this](https://stackoverflow.com/questions/51061645/refresh-php-variable-inside-a-javascript-function)? – TiiJ7 Jun 27 '18 at 13:41
  • 3
    Possible duplicate of [Refresh PHP variable inside a javascript function](https://stackoverflow.com/questions/51061645/refresh-php-variable-inside-a-javascript-function) – IncredibleHat Jun 27 '18 at 13:43
  • 1
    Oddly, all your questions today are about this one problem. – IncredibleHat Jun 27 '18 at 13:45
  • 1
    Wait... what? What does that edit have to do with your original question? Where did that code come from, where was there a mention of a form submit? Or updating a file? – IncredibleHat Jun 27 '18 at 14:07

5 Answers5

2

You can achieve what you want using XMLHttpRequest, like this:

function updateData() {
  var xhttp = new XMLHttpRequest();
  
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById('num').innerHTML = this.responseText;
    }
  };      
  
  xhttp.open('GET', '/num.php', true);
  xhttp.send();
}

setInterval(updateData, 2000);
user7637745
  • 965
  • 2
  • 14
  • 27
1

If you want to refresh the variable without refreshing the page, then you need to use an asynchronous request. It doesn't need to be AJAX, you can use the native XMLHttpRequest() function.

If you want more info: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests

Álvaro Tihanyi
  • 1,062
  • 1
  • 11
  • 18
1

or with jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
    setInterval(function(){
        $.ajax({url:"num.php", success:function(result){
            $("#num").html(result);
        }});
    }, 3000);
</script>

<textarea id="num"></textarea>
MrSmile
  • 1,217
  • 2
  • 12
  • 20
0

like this.

let url = ""; //whatever url your requesting.

setInterval(function(){
    var x = new XMLHttpRequest();
    x.open("GET" , url , true);
    x.onreadystatechange = function(){
        if(x.readyState == 4 && x.status == 200)
        {
             document.getElementById("num").innerHTML = x.responseText;
        }
    }
    x.send();
} , 2000);

-- UPDATE , added post --

setInterval(function(){
        var x = new XMLHttpRequest();
        x.open("POST" , url , true);
        x.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
        x.onreadystatechange = function(){
            if(x.readyState == 4 && x.status == 200)
            {
                 document.getElementById("num").innerHTML = x.responseText;
            }
        }
        x.send("postVar1=123");
    } , 2000);
Hudson
  • 397
  • 1
  • 12
0

get_nbr.php

 <?php
 $file = "num.txt"; //Path to your *.txt file
 $contents = file($file);
 $num = implode($contents);
 echo $num;
 ?>

index.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!---trigger get_nbr() on page load on an infinite loop every two seconds -->
<body onload="setInterval(get_nbr, 2000)">
<script>
  function get_nbr() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
  document.getElementById("num").innerHTML = this.responseText;
  }
  };
  xhttp.open("GET", "get_nbr.php", true);
  xhttp.send();
  }
 </script> 
<p id="num"></p>
AraByte
  • 154
  • 9