Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何实时更新页面并保存jquery更新_Javascript_Php_Jquery - Fatal编程技术网

Javascript 如何实时更新页面并保存jquery更新

Javascript 如何实时更新页面并保存jquery更新,javascript,php,jquery,Javascript,Php,Jquery,我用的是这个代码 <form id="love" name="love" method="post" onsubmit="return false"> Name: <input id="name" type="text" name="name"><br> <input type="submit" name="submit"> </form> <table class="table table-bordered" i

我用的是这个代码

<form id="love" name="love" method="post" onsubmit="return false">
  Name: <input id="name" type="text" name="name"><br>
  <input type="submit" name="submit">
</form>
    <table class="table table-bordered" id="update">
    <thead>
        <th>User</th>
        <th>Bet Amount</th>
        <th>Payout</th>
        <th>Game</th>
        <th>Roll</th>
        <th>Profit</th>
    </thead>
    <tbody>
        <script type="text/javascript">

  $("#love").submit(function() {

    $.ajax({
      type: 'GET',
      url: 'response.php',
      data: {   username: $('#name').val() },
      success: function (data) {
        $("#update").prepend(data);
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError);
      }
    });
  });

</script>
    </tbody>
    </table>

名称:
使用者 下注金额 支出 游戏 卷 利润 $(“#爱”).submit(函数(){ $.ajax({ 键入:“GET”, url:'response.php', 数据:{username:$('#name').val()}, 成功:功能(数据){ $(“#更新”).prepend(数据); }, 错误:函数(xhr、ajaxOptions、thrownError){ 警报(thrownError); } }); });
//Response.PHP

<?php
$username = $_GET['username'];
echo "<tr>";
echo "<td>$username</td>";
echo "</tr>";
?>

这段代码的作用是,当有人输入一个查询时,表中会添加一个具有他输入的名称的新行。问题是,

  • 刷新时,数据将丢失。(即,它仅为客户端更新)

  • 它不会显示给其他用户(因为它只针对客户端)

  • 我如何才能让它为所有在线用户进行更新,同时又不会在刷新时丢失数据(即,更新服务器的数据)


    谢谢

    首先,您需要修改Response.php以包括以下内容:

  • 将新用户名保存到数据库
  • 从数据库中选择现有用户
  • 返回从数据库中获取的所有用户

  • 由于无法从php向浏览器发出推送请求,因此需要每隔2分钟调用以下js(可配置):

    可以通过javascript中的setInterval()函数实现这一点


    注意:我刚刚描述了如何实现解决方案的概念,您需要自己编写代码。如果你仍然面临这个问题,请告诉我

    使用服务器上的数据库,并将答案发布到它。Thansk。但是有没有一种不使用数据库的方法呢?如果您愿意,这种方式可能会变得非常消耗资源,您可以将数据存储在JSON文件或XML文件中,然后跳过使用数据库。好的,我将测试它,过一会儿再返回给您。从现在开始吃午饭什么的
    $username = isset($_GET['username'])? $_GET['username'] : NULL;
    
      if($username) {
           //save new user to database  
      }
    
    //STEP2. pick all the users from database
    
    //STEP3. return all the users obtained from Databse.
    
    ?>
    
    $.ajax({
          type: 'GET',
          url: 'response.php',
          data: {},
          success: function (data) {
            $("#update").html(data);
          },
          error: function (xhr, ajaxOptions, thrownError) {
            alert(thrownError);
          }
        });