Using jquery to refresh page when database changes

I want to refresh the page when my database changes. I want to use jquery for this. The question is not clear? Then take a look at this, suppose this is my page:

<?php $query=mysql_query("select * from tbl1 where user='admin'"); if(mysql_num_rows?($query)!=0) { echo 'Table 1 has values'; } else { echo 'Table1 is empty'; } ?> 

This action should be performed whenever any new record is added to the database. Now suppose I add a record to the database manually, then the page should automatically show the result as "Table1 has values." I know that it can be used using the periodic update page, but I do not want to use it. Instead, I want to try something else, such as an ajax poll? Can someone give me a demo?

+6
source share
3 answers

You can use a lengthy survey, but do a lot of research first. Your server may kill a request that seems open for a long time.

In PHP, your code will look something like this ...

 set_time_limit(0); while (TRUE) { // Query database here if ($results) { echo json_encode($results); exit; } sleep(1); } 
+1
source

You can use the Ajax jQuery Framework with Ajax:

http://www.w3schools.com/Ajax/default.asp
http://api.jquery.com/jQuery.ajax/

It will invoke the server side of the script asynchronously and refresh your page accordingly. You can also use jQuery to specify the update format.

+1
source

You are looking for Ajax-Push / Comet solutions. This is not trivial. You also mentioned ajax union.
Well, on the server side, you need to loop until you have a timeout (which you yourself or the server have determined, make sure you return the HTTP status code for the timeout), or the request can be satisfied. And on the client side, whenever you complete the operation successfully, just process it and then make the same ajax call again, if you time out just repeat the same ajax request until it is satisfied.

0
source

Source: https://habr.com/ru/post/888500/


All Articles