-1

I read a question and this was the answer:

You can read about Jquery Ajax from official jquery Site:

Site Link - >https://api.jquery.com/jQuery.ajax/

If you don't want to use any click event then you can set timer for periodically update.

Below code may be help you just example.

function update() {
$.get("response.php", function(data) {
$("#some_div").html(data);
window.setTimeout(update, 10000);
  });
}

Above function will call after every 10 seconds and get content from response.php and update in #some_div...

Now I am making a chat website. Simple. But if let's say there have been 200 msgs sent from the beginning of the conversation. If I renew the whole of the chat data everytime, it will have to write the 200 msgs again and will then write the 1 new msg i.e. the two hundred and first message. What I mean is that I want only the not-already-present data to be UPDATED not INSERTED.. :) hope you get what I mean. And plus, in all the questions they either used a $.get request or a load() function of jquery writing the path to a mysterious php file but never telling what on earth is in the php file :) plus, please don't block me for seeking help as I was last time :'( It was heart-breaking really to be kicked off such an awesome community :)

Community
  • 1
  • 1

2 Answers2

0

The backend (or server-side) has to be the one to deal with the processing and returning of the updates(the not-already-present-data).

The only thing you can do with the front-end (or client-side) is to get the response from the server, append it to the container.

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
-1

You need to improve your php to return just the desired messages, don't return the html, return json to be easier to work.

file.php

$message1 = "message 1";
$message2 = "message 2";
$allMessages = json_encode(array($message1, $message2));
echo $allMessages;

When ajax is called, request will send something like: {"message1", "message2"}

javascript code

//when document is load
$(function(){
window.setInterval(function(){   
    //do ajax every x seconds     
    $.ajax({
        type: "GET",
        url: "file.php",
        dataType: "json",
        success: function(results){
            //results have a json
            results = JSON.parse(json);
            //iterate each message
            for(var i in results) {
                //append each message to the chat div
                $("#chat").append(results[i]);
            }
        }
    });
}, 1000);

});

I hope that it help you

viniciuswebdev
  • 1,286
  • 1
  • 11
  • 20
  • It doesn't answer how to only get the message(s) that are not already loaded. – putvande Jul 19 '14 at 16:32
  • There is many ways to do this, each time that you iterate the messages, put the id of message inside a javascript array, every ajax call send the list to php, so in this way you can filter the messages that already sent on backend and send only new messages – viniciuswebdev Jul 19 '14 at 16:41
  • i used something like that which vinciuswebdev suggested. thanks for the help every1. Love this community :) – Love Haider Jul 25 '14 at 20:27