subhomoy Posted August 11, 2013 Share Posted August 11, 2013 hello i'm creating an ajax chat system and everything is working fine but the problem, i'm facing is that i can't replace some part of the message with the emotion kept in the array... If i run only the emotion script then its working fine but i can't immplement with the chat system.. Any body plz help... <?php ob_start(); session_start(); ?> <style> body{ background-color: grey; } #box{ width: 326px; height: 432px; border: 1px solid activeborder; margin: 0px auto; } #box1{ width: 326px; height: 400px; background-color: #eee; margin-bottom: 10px; } </style> <form method="post" action=""> <div id="box"> <div id="box1"> <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db(chat) or die(mysql_error()); $req = mysql_query("SELECT * FROM msg") or die(mysql_error()); while($row = mysql_fetch_array($req)){ $m = $row['message']; echo "<div id='message'> $m </div>"; } ?> </div> <input type="text" id="chat" size="50" /> <input type="button" name="submit" value=" Chat " onclick="sendmessage()" /> </div> </form> <script> function sendmessage() { var msg = document.getElementById("chat").value; var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("box1").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","chat_msg.php?m="+msg,true); xmlhttp.send(); } </script> and the chat_msg.php code is <?php session_start(); mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db(chat) or die(mysql_error()); $sess = session_id(); $mg = $_REQUEST['m']; $sql = mysql_query("INSERT INTO msg VALUES('','$sess','$mg')") or die(mysql_error()); $req = mysql_query("SELECT * FROM msg") or die(mysql_error()); while($row = mysql_fetch_array($req)){ $m = $row['message']; echo "<div id='message'> $m </div>"; } ?> and the emotion code is $str = $_REQUEST['comment']; $emo = array("<3", "#12", "@153", "#45", "@352"); $img = array("<img src='emotions/1.png' height='113' width='120' alt='ugly' />", "<img src='emotions/2.png' height='113' width='120' alt='happy' />", "<img src='emotions/3.png' height='113' width='120' alt='love' />", "<img src='emotions/4.png' height='113' width='120' alt='sweet' />", "<img src='emotions/5.png' height='113' width='120' alt='smiley' />"); $new_str = str_replace($emo, $img, $str); echo "<hr />"; echo $new_str; } ?> i can;t figure where to put the emotion code so that it will work like facebook chat system. Any help will be greatly appreciated... Thank u. Quote Link to comment https://forums.phpfreaks.com/topic/281050-ajax-chat/ Share on other sites More sharing options...
limitbreaker Posted August 12, 2013 Share Posted August 12, 2013 (edited) hello i'm creating an ajax chat system and everything is working fine but the problem, i'm facing is that i can't replace some part of the message with the emotion kept in the array... If i run only the emotion script then its working fine but i can't immplement with the chat system.. Any body plz help... <?php ob_start(); session_start(); ?> <style> body{ background-color: grey; } #box{ width: 326px; height: 432px; border: 1px solid activeborder; margin: 0px auto; } #box1{ width: 326px; height: 400px; background-color: #eee; margin-bottom: 10px; } </style> <form method="post" action=""> <div id="box"> <div id="box1"> <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db(chat) or die(mysql_error()); $req = mysql_query("SELECT * FROM msg") or die(mysql_error()); while($row = mysql_fetch_array($req)){ $m = $row['message']; echo "<div id='message'> $m </div>"; } ?> </div> <input type="text" id="chat" size="50" /> <input type="button" name="submit" value=" Chat " onclick="sendmessage()" /> </div> </form> <script> function sendmessage() { var msg = document.getElementById("chat").value; var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("box1").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","chat_msg.php?m="+msg,true); xmlhttp.send(); } </script> and the chat_msg.php code is <?php session_start(); mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db(chat) or die(mysql_error()); $sess = session_id(); $mg = $_REQUEST['m']; $sql = mysql_query("INSERT INTO msg VALUES('','$sess','$mg')") or die(mysql_error()); $req = mysql_query("SELECT * FROM msg") or die(mysql_error()); while($row = mysql_fetch_array($req)){ $m = $row['message']; echo "<div id='message'> $m </div>"; } ?> and the emotion code is $str = $_REQUEST['comment']; $emo = array("<3", "#12", "@153", "#45", "@352"); $img = array("<img src='emotions/1.png' height='113' width='120' alt='ugly' />", "<img src='emotions/2.png' height='113' width='120' alt='happy' />", "<img src='emotions/3.png' height='113' width='120' alt='love' />", "<img src='emotions/4.png' height='113' width='120' alt='sweet' />", "<img src='emotions/5.png' height='113' width='120' alt='smiley' />"); $new_str = str_replace($emo, $img, $str); echo "<hr />"; echo $new_str; } ?> i can;t figure where to put the emotion code so that it will work like facebook chat system. Any help will be greatly appreciated... Thank u. Not seeing where $_request['comment'] comes into play, so I'm assuming that's supposed to be the same message that goes through chat_msg.php. First of all, you should get rid of the AJAX request in the original HTML. I'd keep the text in the database as it is, and place the emote code right before you echo in chat_msg: <?php session_start(); mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db(chat) or die(mysql_error()); $sess = session_id(); $mg = $_REQUEST['m']; $sql = mysql_query("INSERT INTO msg VALUES('','$sess','$mg')") or die(mysql_error()); $req = mysql_query("SELECT * FROM msg") or die(mysql_error()); while($row = mysql_fetch_array($req)){ $m = $row['message']; $emo = array("<3", "#12", "@153", "#45", "@352"); $img = array("<img src='emotions/1.png' height='113' width='120' alt='ugly' />", "<img src='emotions/2.png' height='113' width='120' alt='happy' />", "<img src='emotions/3.png' height='113' width='120' alt='love' />", "<img src='emotions/4.png' height='113' width='120' alt='sweet' />", "<img src='emotions/5.png' height='113' width='120' alt='smiley' />"); $new_str = str_replace($emo, $img, $m); echo "<hr />"; echo $new_str; } ?> Edited August 12, 2013 by limitbreaker Quote Link to comment https://forums.phpfreaks.com/topic/281050-ajax-chat/#findComment-1444485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.