【问题标题】:Repeat PHP function every 2 seconds每 2 秒重复一次 PHP 函数
【发布时间】:2018-06-27 13:35:13
【问题描述】:

所以我有一个代码,其中一个数字是从文本文件中检索并通过 HTML 显示的。

问题是它只在我刷新页面时检索数字。如果我更改其他页面上的数字,它不会在其他页面上更改。

之前也看到过类似的问题,大部分都说用AJAX,但是我对AJAX不是很了解,只需要这段代码,不会一直用.我想知道除了AJAX还有没有其他的,如果只有AJAX,请提供代码示例。

PHP 代码:

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

JavaScript 代码。基本上它获取 PHP 值并输出到 html。

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

请记住,我不想刷新页面。只是变量。


编辑: PHP 代码 - 出现错误 - 注意:未定义索引:第 3 行 C:\xampp\htdocs\num.php 中的 keynum

这是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();
 ?>

【问题讨论】:

标签: javascript php ajax xmlhttprequest


【解决方案1】:

你可以使用XMLHttpRequest实现你想要的,像这样:

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);

【讨论】:

  • 注意:未定义索引:keynum in C:\xampp\htdocs\num.php on line 3 Response
  • 我得到了那个错误。 num.php 是写入 txt 的 php 文件。这是代码
  • 请用您刚刚在上面发布的 PHP 代码更新您的问题,并注明您遇到的任何错误,以便我们更轻松地为您提供帮助。谢谢!
  • 已编辑。感谢您的帮助
【解决方案2】:

如果要在不刷新页面的情况下刷新变量,则需要使用异步请求。不需要是AJAX,可以使用原生的XMLHttpRequest()函数。

如果您想了解更多信息:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests

【讨论】:

    【解决方案3】:

    或使用 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>
    

    【讨论】:

    • 它在 num.php 上给出了这个错误 注意:未定义的索引:第 3 行 C:\xampp\htdocs\num.php 中的 keynum
    • @MiguelCasanova 但我没有给你 num.php 的代码 :)
    • @MiguelCasanova 因为没有带有索引 keynum 的 POST 变量。您想向脚本发送什么信息?
    【解决方案4】:

    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>
    

    【讨论】:

    • 值不变。
    • 请记住,php 代码与 javascript 位于同一文件中。
    • 嗯,我的答案是完美的基于拥有所有三个文件的事件:get_nbr.php、index.php 和 num.txt .... 你想用你的数字执行任何操作吗?得到?
    • 可以正常工作,但是,当我不更改值时,它会消失,但如果我不断更改它,它会正常工作。
    • 终于工作了。非常感谢。
    【解决方案5】:

    像这样。

    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);
    

    -- 更新,添加帖子--

    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);
    

    【讨论】:

    • 注意:未定义的索引:第 3 行响应中 C:\xampp\htdocs\num.php 中的 keynum。我得到那个错误。 num.php 是写入 txt 的 php 文件。这是代码
    • postVar1 = $_POST['postVar1'] in php ,只需换掉值
    • 似乎没有用。没有给出错误,但值没有改变。
    • var_dump 你的帖子变量
    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多