how to update content automatically without reloading webpage using php/ajax?

前端 未结 3 1603
清酒与你
清酒与你 2021-01-14 04:04

I\'m trying to create an auction tool using PHP. The problem I\'m having (and I appreciate its a basic one but I need clarification) is that I don\'t understand how to updat

相关标签:
3条回答
  • 2021-01-14 04:22

    Php/Ajax example:

    In this example you have index.html and record_count.php files

    Here is the Code:

    index.html contains the html code and javascript call to load record_count.php

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript">
       var auto_refresh = setInterval(
          function ()
          {
             $('#load_tweets').load('record_count.php').fadeIn("slow");
          }, 10000); // refresh every 10000 milliseconds
    </script>
    
    <body>
         <div id="load_tweets"> </div>
    </body>
    

    and record_count.php has the php code

    <?php
    echo "some code or variable here";
    ?>
    

    you can change the javascript interval to suit your needs

    I'll leave you the blog link as a reference: 9lessons

    0 讨论(0)
  • 2021-01-14 04:23

    As I making interactive displays, which must switch pages instantly, then I create pages without refreshing.

    My approach is something like that:

    1. I have one index.html with all structure (pages) with all necessary html tags.
    2. javascript/typescript loads json from CMS (Kirby for example), which has all information about texts and image links.
    3. when json is loaded now I just need to switch between pages by showing/hiding or creating/removing html elements. And all data and texts are loaded by javascript.

    There is some cons, which can be fixed, for example link for each page in address bar. In that case You need to add history management and change url in address bar on page switch.

    0 讨论(0)
  • 2021-01-14 04:30

    Ther question you asked has so much possible answers, they could fill a whole book.

    The simplest way to do this is to make an ajax call every few seconds using a combination of the setInterval() function and AJAX calls. You basically make an AJAX request every few seconds:

    setInterval(function(){
    $.get( "anyChanges.php", function( data ) {
      //do something with the returned data. Maybe update a table or something
    });
    }, 3000);
    

    On server side anyChanges.php returns some data immediately, like confirmation that something has changed and the new data.

    Long polling is how Google and others do it. It's the same as the example above. The difference is on the server side. anyChanges.php would not return immediately, the script would keep the connection open until there is some new changes and return them. If you use long polling, you usually set the interval to longer, for example 30 seconds.

    The best way to do it in my opinion, are WEB Sockets. It is a very new technology. With web sockets you can create a two-way connection to the server. That means that the server could simply send data to the clients without them having to ask for new data every few seconds. In PHP it's a little difficult to use web sockets (Or so I heard), but you could give it a shot. If you choose web sockets, try to learn about them first: tutsplus tutorial

    This library will be helpfull: socketo.me

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