dongying195959 2015-11-29 11:05
浏览 110
已采纳

刷新div以从数据库中检索信息而不刷新整个页面

I would like to refreash the div auto every 2 seconds without reloading the page how can i do this with jquery i did try some solutions but they did not work if can help that would be great

conversation.php

<?php
        $hash =$run_con['hash'];
        while($run_message = mysql_fetch_assoc($message_query))
            {

                 $from_id = $run_message['from_id'];
                 $message = $run_message['message'];

                $user_query = mysql_query("SELECT `username` FROM `users` WHERE `id`='$from_id'");
                $run_user = mysql_fetch_array($user_query);
                $from_username = $run_user['username'];


                echo "<div id='auto'<p><b>$from_username</b><br />$message</p>  </div>"; //reload this part every 2 seconds


            }
    ?>

<script>
    $(document).ready(function(){
    setInterval(function(){
    $("#auto").load('conversation.php'+$hash)
    }, 2000);
    });
 </script>
  • 写回答

2条回答 默认 最新

  • duaj39673 2015-11-29 14:07
    关注

    In order to make a query every 2 seconds that doesn't refresh the page itself, you'll need to use jQuery and AJAX. For this to work, you should create a file of it's own with the query that is being called every 2 seconds.

    Let's start with the jQuery.

    For this script to actually work, you'll need to include jQuery. Inside the HTML <head></head>-tags on your page, include the following line

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
    

    Without this, you can't run any jQuery functions. Now that you have included this, you can build the actual script.

    To explain a bit on how this works, I've created a function called ajaxCall. This function executes the script inside ajax.php, and when it's successful with that, it replaced the output from ajax.php with the content on your div that has an id div1, on the current page. This action is performed once when the page is loaded (ajaxCall();) and every 2 seconds after that.

    <script>
    function ajaxCall() {
        $.ajax({
            url: "ajax.php", 
            success: (function (result) {
                $("#div1").html(result);
            })
        })
    };
    
    ajaxCall(); // To output when the page loads
    setInterval(ajaxCall, (2 * 1000)); // x * 1000 to get it in seconds
    </script>
    

    Place this script in the same file where you want the div1 to be reloaded (of course you can rename it as whatever you please, just be persistent).

    So your conversation.php should now have...

    • included the jQuery libary
    • the script to perform the AJAX-call
    • a div with id "div1" (<div id="div1"></div>), where the contents of ajax.php will be put.

    As for your ajax.php file, this is where you'll actually perform your query. For your case, that would be where you put

    <?php
    mysql_connect("localhost", "user", "pass");
    mysql_select_db("database");
    
    // Other variables needed to perform the query, such as $message_query
    // needs to be here as well
    
    while ($run_message = mysql_fetch_assoc($message_query)) {
        $from_id = $run_message['from_id'];
        $message = $run_message['message'];
    
        $user_query = mysql_query("SELECT `username` FROM `users` WHERE `id`='$from_id'");
        $run_user = mysql_fetch_array($user_query);
        $from_username = $run_user['username'];
    
        echo "<p><b>$from_username</b><br />$message</p>";
    }
    ?>
    

    You should stop using mysql_* functions if you can, as they are deprecated and no longer maintained. In addition, usage of mysqli_* or PDO will enable usage of prepared statements, so that you can prevent SQL-injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line