Update Content After Certain Time using jQuery,Ajax and PHP

Some content updates may be based on time the user information for the current user, other updates may be based on requests performed by any user, such as information based on a search performed by the Web-site visitor. You may also like Mysql UPDATE Statement using Join Statement.

HTML and jQuery Code

<html>
    <head>
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>

    <body>
        <div id="quote_box">
            <p id="quote">Please Wait</p>
        </div>
    </body>
    <script type="text/javascript">
        $(document).ready(function() {
            $.ajaxSetup({ cache: false }); 
            setInterval(fetch_quotes,5000);
        });

        function fetch_quotes()
        {
            $.ajax({
            type: 'post',
            url: 'get_content.php',
            data: {
             get_quote:"quote"
                },
                success: function (response) {
                    document.getElementById("quote").innerHTML = '"' + response + '"';
                }
            });
        }
    </script>
      
</html>

PHP Code (get_content.php)

<?php
if (isset($_POST['get_quote'])) {
    $quote = array(
        'demo1',
        'demo2',
        'Success'
    );

    $total = count($quote) - 1;
    $var = mt_rand(0, $total);
    echo $quote[$var];
    exit();
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *