1

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.

Jonathan
  • 49
  • 1
  • 8

2 Answers2

2

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) {

        }
    });
Gabriel Rodriguez
  • 1,163
  • 10
  • 23
0

You could use HTML and the refresh tag to refresh the entire page, or you could use JS to check for new messages at an interval and, with some ajax, only refresh the portion of the page that displays the message.

StephenCollins
  • 785
  • 4
  • 10