简体   繁体   中英

How to refresh a part of a webpage?

I have a PHP page which queries some data from a PostgreSQL database. The data must be always shown up to date, and thus I must refresh it every single second. Right now I'm using but it refresh the entire page, which is not what I want to.

What matters to me is actually in here

I already looked it up and even saw some posts in here, but nothing suited me very well yet. For now (as matter of learning, I'd say) I don't want to get all the PHP query statements, put in another PHP file and refresh it.

I do want a solution for refreshing partially a PHP part of a page.

The best way to do this is using AJAX. (Sending requests in the background via JavaScript, and loading the results into the webpage)

There are a lot of TuTs out there.

If you use JQuery, and create a Php Script which when called will print out the data as you like.

Using the $.ajax feature of JQuery will enable to you dynamically load that page, then you can insert it into the page using $('#MyID').html('New Content'); .

An example of this, using a script called "refresh.php" with the a dynamic variable of 'ID', would be

$.post('refresh.php', { ID: 1 }, function(result){
    $('#MyID').html(result);
}

You could also use json_encode() to separate the output data, via using the JQuery function $.parseJSON

An example of this would be

$.post('refresh.php', { ID: 1 }, function(result){
    var data = $.parseJSON(result);
    $('#MyID').html(data.ID);
}

That would access the JSON encoded variable of "ID", returned from PHP.

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