dongtiao5094
2016-10-10 20:13
浏览 78
已采纳

如何在PHP中回显更新变量

Thanks for coming to try and help me solve my problem!

My problem is, I would like the display a variable on my page using PHP. This variable updates every second or so. I would like this to update on the page as the variable changes. I tried using a while loop but this just continuously prints the variable. I will warn you, I am not very PHP savvy, and you're probably going to have to explain your answer to me, A LOT. Sorry I didn't include any code samples.. I didn't really think it was needed for this question. If you would like me to provide some samples, I will though. Thanks for considering answering my question! :)

图片转代码服务由CSDN问答提供 功能建议

感谢您来帮我解决问题!

我的 问题是,我希望使用PHP在我的页面上显示一个变量。 此变量每秒更新一次。 我希望在变量更改时在页面上更新。 我尝试使用while循环,但这只是连续打印变量。 我会警告你,我不是很精通PHP,你可能不得不向我解释你的答案,很多。 对不起,我没有包含任何代码示例..我真的不认为这个问题需要它。 如果您希望我提供一些样品,我会的。 感谢您考虑回答我的问题! :)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucheng5209 2016-10-10 20:30
    已采纳

    You try to do something which is not so easy to implement with the basics of PHP. PHP is once generated on the server side and then gives the result to the client. (your browser). If you like to change the HTML Code dynamically you should take a look at JavaScript. This will allow you to make dynamic updates without reloading the page. (e.g. a user presses a button or a timer in your case):

    var i = 1;
    
    // update site content every second
    setInterval(function() {
    
      // get div with id example
      var myElement = document.getElementById('example');
    
      // count up by 1
      i++;
    
      // update content
      myElement.innerHTML = 'Zahl ' + i;
    
    }, 1000);
    <html>
    
    <head>
      <meta charset="UTF-8">
      <title>JavaScript test</title>
    </head>
    
    <body>
      <div id="example">Zahl 1</div>
    </body>
    
    </html>

    Now if you would like to run a PHP Script every second you need something else which is called AJAX. So for example if you would like to run hello.php every one second and set the result of it to the same div from before you need this code:

    var i = 1;
    
    // update site content every second
    setInterval(function() {
    
      // run function loadDoc every second
      loadDoc();
    
    }, 1000);
    
    function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        // get div with id example and update HTML
        document.getElementById("example").innerHTML = this.responseText;
      };
      xhttp.open("GET", "hello.php", true);
      xhttp.send();
    }
    <html>
    
    <head>
      <meta charset="UTF-8">
      <title>JavaScript test</title>
    </head>
    
    <body>
      <div id="example">Zahl 1</div>
    </body>
    
    </html>

    </div>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanhe6799 2016-10-10 20:19

    If you want the updated variable to be shown in your browser every time, you would have to reload your client. You can do this by redirecting to the same page once the variable is updated in your PHP code.

    Or you could use Ajax on your client side, which continuously sends a request to the server to get the value of the variable and then prints it in the browser.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题