开发者

use jquery ajax to refresh PHP data on same page

开发者 https://www.devze.com 2023-02-04 09:00 出处:网络
I have a PHP page with a mysql connection, a select query and then i\'m building a table using PHP. If i wanted to use jQuery AJAX to refresh the data on a setInterval, on开发者_如何学编程 the same pa

I have a PHP page with a mysql connection, a select query and then i'm building a table using PHP. If i wanted to use jQuery AJAX to refresh the data on a setInterval, on开发者_如何学编程 the same page, how would i go about doing that? (by the way i can do it to another PHP page but i've never done it if the PHP stuff is on the same page)


If you want to keep making ajax calls i suggest you do long polling, which basically means you have a script that is requesting content via ajax every given time, and it will verify every time if the content has been modified, if not it will wait again and make another call.

I used jQuery Periodical updater for a chat box and it worked perfectly.

If you wnat to learn more about how jQuery and AJAX work, check out this Nettus article


You set the param in request, for instance file.php?ajax=1

The, depending on its value, you render full html or just the necessary elements for ajax

in php:

if($_GET['ajax']) renderAjax();
else renderFullHTML();

in js:

$.get('file.php?ajax=1', function(data) {
  $('.result').html(data);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消