javascript - determining sender and recipient of a message in a chat application PHP AJAX -
currently working on chat app allows private chat messaging.
the challenge :
if 2 users starts private chat, how determine , fetch unique id of sender , receiver between both parties.
brief on how i'm developing app:
within chat box, have appended id of both parties it.
the $userid id of user initiated or started private chat while $rec_id other party whom $userid want chat with.
just blank on determining sender of message , recipient challenge.
js
//handles user pressing enter. instead of submitting form, new message sent server , return false. function blocksubmit() { sendchatmsg(); return false; } //send message server. function sendchatmsg() { if(document.getelementbyid('new_message').value == '') { alert("you did not type message!"); return; }else{ if(sendnewmsg.readystate == 4){ //sendnewmsg.open("post", 'inc/chat_processor.php?chat_msg&rec='+ recidvar, true); sendnewmsg.open("post", 'inc/chat_processor.php?send_chat_msg', true); sendnewmsg.setrequestheader('content-type','application/x-www-form-urlencoded'); //var newchatmsgvar = 'send_chat_msg'; //newchatmsgvar += '&new_msg=' + document.getelementbyid('new_message').value; var newchatmsgvar = '&new_msg=' + document.getelementbyid('new_message').value; newchatmsgvar += '&new_sender=' + document.getelementbyid('new_msg_sender').value; newchatmsgvar += '&new_receiver=' + document.getelementbyid('new_msg_receiver').value; sendnewmsg.send(newchatmsgvar); document.getelementbyid('new_message').value = ''; sendnewmsg.onreadystatechange = function(){ if(sendnewmsg.readystate == 4 && sendnewmsg.status == 200){ //clear out existing timer avoid multiple timer instances running before chat box update. clearinterval(mtimer); // update chat box after message has been sent. getchattext(recidvar); } }// end: undefined callback chat message sending } } }
php
if(isset($_get['send_chat_msg'])) { $new_msg = @mysql_real_escape_string(strip_tags($_post['new_msg'])); $new_sender = @mysql_real_escape_string(strip_tags($_post['new_sender'])); $new_receiver = @mysql_real_escape_string(strip_tags($_post['new_receiver'])); $qry_sender_det = @mysql_query("select * `member_bio` m_id = '".$user_id."'"); $sender_det_row = @mysql_fetch_assoc($qry_sender_det); $sender_name = $sender_det_row['f_name']." "; $sender_name .= $sender_det_row['l_name']; $sender_pix = $sender_det_row['profilepic']; $sql = "insert `inv_chat` (`id`, `sender_id`, `receiver_id`, `msg`, `name`, `profilepic`, `cur_time`, `cur_date`) values ('', '$new_sender', '$new_receiver', '$new_msg', '$sender_name', '$sender_pix', curtime(), curdate());"; @mysql_query($sql); }
html
<?php @include "inc/db.php"; @include "inc/user_login_settings.php"; $userid = useraccessinfo('id'); $rec_id = @mysql_real_escape_string(strip_tags($_get['rec_id'])); $qry_result = @mysql_query("select * `member_bio` m_id = '$rec_id'"); $result_row = @mysql_fetch_assoc($qry_result); $rec_name = $result_row['f_name']." "; $rec_name .= $result_row['l_name']; ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title><?php echo $rec_name; ?></title> <script type="text/javascript" src="js/getchatmsg.js"></script> <style> * {padding:0; margin:0;} .new_chat_main_holder {padding:5px;} form input {border:1px solid #090; width:95%; height:30px; padding:5px;} div.chat_msg_row { padding-left:5px; padding-top:10px; padding-bottom:3px; border-bottom:1px dashed #060;} div.chat_msg_row img {float:left; margin-right:10px; width:30px; height:30px;} div.chat_msg_row p strong {font-size:12px; color:#090; margin-bottom:3px; font-weight:bold; color:#900;} div.chat_msg_row p {font-size:12px; color:#090; margin-bottom:3px;} div.chat_msg_row p span {font-size:10px; font-style:italic; color:#000;} </style> </head> <body onload="javascript:allchatmsgloader(<?php echo $rec_id; ?>);"> <div class="new_chat_main_holder"> <div id="new_chat_box" style="width:99%; height:290px; overflow: auto; background-color: #fc6; border: 1px solid #555555;"> </div> <form id="frmmain" name="new_chat_msg_submit" onsubmit="return blocksubmit();"> <input type="text" id="new_message" name="new_message" placeholder="type chat message here" autocomplete="off" maxlength="250" /> <input type="hidden" id="new_msg_sender" name="new_msg_sender" value="dont know place here" /> <input type="hidden" id="new_msg_receiver" name="new_msg_receiver" value="dont know place here" /> </form> </div> </body> </html>
please note: im using pop-up window private chat, stil working on jquery dialog/modal box.
would glad , grateful getting help.
Comments
Post a Comment