简体   繁体   中英

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

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 update the "auction price" automatically on each users screen without them having to take any action or without causing a full reload of the page.

So far I understand that Ajax is used to do this but if anyone could point me in the right direction or to any useful materials. My plan for my project so far is to use PHP and JavaScript so any solution would need to be compatible with these languages.

Note: I'm using a MySQL database.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM