0

So recently I improved my code and managed to save a javascript variable through PHP(AJAX) to a txt file. The txt is working and it gets the javascript number but I'm having problems retrieving the number on the txt file.

PHP code:

<?php
 $file_content = file_get_contents('num.txt');
?>

Javascript code:

function recnum(){
document.getElementById("num").innerHTML = '<?php echo $file_content; ?>';
}

So after this, the number on the html page stays the same (1) and doesn't change.

  • What is the content of the text file? – Can O' Spam Jun 27 '18 at 09:55
  • Why should the number change? You overwrite the text file? How? Have you verified that the content of the file changes? Have you refreshed the webpage? [F5] – KIKO Software Jun 27 '18 at 09:55
  • Yes the file is changing correctly. The content is a number and it's always changing when I press the button, but the error is that I can't read that number on the text file. – Miguel Casanova Jun 27 '18 at 09:56
  • 1
    If the result is `1` then the script has read something... namely `1`. Check that you're actually reading the file you wrote, and again: Make sure the page is refreshed properly (and not cached). – KIKO Software Jun 27 '18 at 09:59
  • The result is 1 because the output on the HTML page is

    1

    It is not getting the new value given by the text file.
    – Miguel Casanova Jun 27 '18 at 10:02
  • Is there anyway I can avoid the javascript, like put directly the php into the output? – Miguel Casanova Jun 27 '18 at 10:04
  • 3
    I suspect there may be a misunderstanding about [client and server-side programming](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) here. – CD001 Jun 27 '18 at 10:12
  • " It is not getting the new value given by the text file"...if you updated the text file _after_ you loaded the page then naturally it will not read the new value. The value of PHP `$file_content` variable was injected into the HTML/JS by PHP at the time that PHP created the content and sent it to the browser. So you will end up with JS code like `document.getElementById("num").innerHTML = '1'` in your browser. When you run the JS `recnum()` function it simply repeats inserting that hard-coded value. The PHP code does not get executed again until you make another request to re-load the page. – ADyson Jun 27 '18 at 10:24
  • Are you executing recnum(), because this js code is only a function declaration. – Emeeus Jun 27 '18 at 10:44
  • IMHO, use jQuery and an Ajax call to fetch the latest value from the file. – Adder Jun 27 '18 at 10:54

1 Answers1

1

If you want to put data directly into HTML avoiding Javascript then you can try the code following:

<div id="num"><?php echo $file_content; ?></div>

Note: I assumed that you have div with num id already in html.

UPDATE: Based on your comment. You have defined variable after it being used. So define it first and then use like in modified code below:

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

<html> 
    <body> 
    <head> 
        <title> Botão </title> 
        <link rel="stylesheet" href="css/style.css">
        <meta charset="UTF-8"> 
        <script type="text/javascript" src="jquery-3.3.1.min.js"></script> 
    </head>
    <div id="num"><?php echo $file_content; ?></div>
</html>
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36