javascript - Live updater chat script -



javascript - Live updater chat script -

so i'm kind of new php, , i'm trying write php live chat web application... stores chat info in mysql database. phone call updatedb() function typed out below every couple seconds refresh chat content shows in div have php code in.(the chat info echoed mysql table in div)... every time calls updatedb() function, if have text input type message focused(selected), blurs , can never type message in, because keeps deselecting it. :( ... have improve codes update content that's in div? appreciate help give... (keep in mind, i'm not pro in php, , don't know much javascript). if need post whole document code, can, if don't know mean... whole thing in php if can, don't know if there's way to.

function updatedb() { if (window.xmlhttprequest){ // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest();}else{ // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200){ document.getelementbyid("messages").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","chat2.php",true);xmlhttp.send(); }

you can take 2 approaches here:

1) ajax request server json output of messages, newer message_date. create html wrapper json info , append messages list (sorry, utilize jquery):

$.ajax({ url: 'ajax/getmessages', type: 'post', data: {last: $("#lastmessagedate").val()}, success: function (newmessages) { // newmessages contains json of messages $.each(newmessages, function(key, message){ $("#messages").append("<div class='message'>" + message.content + "</div>"); }); } }); <div id="messages"> <div class="message"> <?php echo $message['content']; ?> </div> <input type="hidden" value="2014-10-20 12:52" id="lastmessagedate"/> </div> <textarea></textarea>

2) utilize same view, reload single element. (again jquery)

$("#messages").load(window.location.href + " #messages > *"); <div class="messages"> <?php foreach ($dbmessages $message) { ?> <div class="message"><?php echo $message['content']; ?></div> <?php } ?> </div> <textarea></textarea>

javascript php html ajax

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -