简体   繁体   中英

update new records in a php page from SQL without reloading

I usually do android app dev and i have no idea about php. I have this php page(created using php maker) which displays a record from my MySQL database. I have to refresh the page every time or keep it to reload every 'x' seconds to see new records from my DB. How can i set that certain page to update the records without reloading the page using jQuery every 'x' second.

First

You will need to put your php in a seperate file and echo the data back, something like this.

myfile.php

<?php
    $sql = "SELECT * FROM table WHERE ID = '$id'";
    $result = $db->query($sql);
    while ($row = $result->fetch_array()) {
        echo $row['something'];
    }
?>

Use setInterval in jquery to make a call to that php file every x seconds, in this case 5 seconds.

jquery

$(document).ready( function() {
    setInterval( function () {
        $('#someidtoloadto').load('/path/to/myfile.php');
    }, 5000); // loads every 5 seconds
});

Then have a div or whatever you want the data to be rendered too.

html

<div id="someidtoloadto"></div>

You can use jQuery ajax function to send request after every 'x' seconds to update the page and update the fetched data on the page.

$.ajax({
    url: "update.php",
    type: "POST",
    data: {id : id},
    cache: false,
    success: function(html){
        $("#results").append(html);
    }
});

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