简体   繁体   中英

Auto reload a piece of code in PHP using Ajax maybe?

Hello I need the value of nextid to be reloaded without refreshing the whole page. How can I do this? Thanks!

global $wpdb;
$query = "SELECT MAX(ID) FROM $wpdb->posts";
$currid = $wpdb->get_var($query);
$nextid = $currid+1;

Yes, and you would use ajax.. the life cycle would go something like this:

  1. Browser requests page

  2. PHP runs and returns html/javascript/css/etc.

  3. javascript runs in browser, using ajax calling a php script on the server

  4. php script returns the results as text or html or json or some other format

  5. javascript takes the results, clears out the old information on the page and puts in the new information

  6. goto 3

There are a ton of javascript libraries out there to help with that part of things. I have used jQuery and highly recommend it.

Update : A simple ajax example:

jQuery(document).ready(function() {
    jQuery.ajax({
        url: 'page.php',
        error: function(jqXHR, textStatus, data) {
            // error
            alert(jqXHR.responseText);
        },
        success: function(data, textStatus, jqXHR) {
            // result
            alert(data);
        }
    });
});

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