refresh div to retrieve information from database without refreshing wholepage

前端 未结 2 1091
一向
一向 2021-01-07 04:07

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

相关标签:
2条回答
  • 2021-01-07 04:31

    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.

    0 讨论(0)
  • 2021-01-07 04:35

    Change this line

    $("#auto").load('conversation.php'+$hash)}, 2000);
    

    to this

    $("#auto").load('conversation.php'+'<?php echo $hash?>')}, 2000);
    

    Because your $hash is PHP variable, but you are not inside a PHP block, you are unable to access it.

    0 讨论(0)
提交回复
热议问题