javascript - AJAX whenever I try to submit my text my div area duplicates the current data displayed plus my new data -
hi have working script of retrieving json data result small chat application problem displays duplicate result of text posted plus newly text inserted db
here whole javascript code:
function sendchattext() { if (sendreq.readystate == 4 || sendreq.readystate == 0) { sendreq.open("post", 'includes/getchat.php?last=' + lastmessage, true); sendreq.setrequestheader('content-type','application/x-www-form-urlencoded'); sendreq.onreadystatechange = ajaxretrieve(); var param = 'message=' + document.getelementbyid('txta').value; param += '&name='+user; param += '&uid='+uid; param += '&rid='+document.getelementbyid('trg').value; sendreq.send(param); document.getelementbyid('txta').value = ''; } } function ajaxretrieve() { var rid = document.getelementbyid('trg').value, data = {chat: uid, rid: rid, name: user}; $.ajax({ url: "includes/getchat.php", type: "get", data: data, datatype: 'json', success: function(result){ $.each(result, function(rowkey, row) { $("#clog").append($('<p />').html('<h4>'+ row.username +':</h4>' + row.message_content) ); }); } }); }
i think getchat.php returns chat text user, appending them #clog.
i have 2 suggestions
return latest text server sending time stamp js like
data = {"chat": uid, "rid": rid, "name": user, "last_read_time": window.last_read_time}; window.last_read_time = new date().format("d/m/yyyy hh:mm:ss tt"); //store new time
clear #clog before looping data
$("#clog").empty(); //clear previous msgs $.each(result, function(rowkey, row) { $("#clog").append($('<p />').html('<h4>'+ row.username +':</h4>' + row.message_content); });
Comments
Post a Comment