How do I retrieve data from the database without interaction?

advertisements

Simple question, how do i retrieve data from database without any interaction?

example: Someone sent me a message and i would like to read it without updating the page.

I have succeeded to insert data into database without updating the page but now i have to retrieve it.


Well you can do a simple script on the backend called getnewinfo.php and send with a simple ajax call, the last message id you printed on your page(ie:hidden on a input text) so the script will return any new message with a date or id greater than the one you are sending. Then simply update any element on html dom, lets say a div with a simple jquery append. Your HTML:

<div id="message">My current message</div>
<input type="hidden" name="" value="242" id="lastid">

Your Javascript:

$.ajax({
        url: "getnewinfo.php",
        data: {
            lastid: $('#lastid').val() //send to your php the last id you printed so it search for any new id different than that one
        },
        type: "GET",
        dataType: "html",
        success: function (data) {
            $('#message').append(data); //Append the new received data
            //Or replace the current value with the new one
            $('#message').html(data); //Replace with the new received data
        },
        error: function (xhr, status) {
            alert("Sorry, there was a problem!");
        },
        complete: function (xhr, status) {

        }
    });