title: determining sender and recipient of a message in a chat application PHP AJAX
body: <p>Currently working on a chat app that allows private chat messaging.</p>
  
  <p>The challenge is :</p>
  
  <p>If two users starts a private chat, how do I determine and fetch the unique id of the sender and the receiver between both parties.</p>
  
  <p>Brief on how I'm developing the app:</p>
  
  <p>Within the chat box, i have appended the id of both parties to it.</p>
  
  <p>The $userId is the id of the user that initiated or started the private chat while $rec_id is for the other party whom the $userId want to chat with.</p>
  
  <p>Just blank on determining who the sender of a message is and who the recipient is the challenge.</p>
  
  <p>JS</p>
  
  <pre><code>//handles user pressing enter. Instead of submitting the form, new message is sent to the server and then return false.
  function blockSubmit() {
      sendChatMsg();
      return false;
  }
  
  
  
  
  //send a message to the server.
  function sendChatMsg() {
      if(document.getElementById('new_message').value == '') {
          alert("You did not type any message!");
          return;
      }else{
              if(sendNewMsg.readyState == 4){
                  //sendNewMsg.open("POST", 'inc/chat_processor.php?chat_msg&amp;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 += '&amp;new_msg=' + document.getElementById('new_message').value;
                  var newChatMsgVar = '&amp;new_msg=' + document.getElementById('new_message').value;
                  newChatMsgVar += '&amp;new_sender=' + document.getElementById('new_msg_sender').value;
                  newChatMsgVar += '&amp;new_receiver=' + document.getElementById('new_msg_receiver').value;
                  sendNewMsg.send(newChatMsgVar);
                  document.getElementById('new_message').value = '';
  
                  sendNewMsg.onreadystatechange = function(){
                      if(sendNewMsg.readyState == 4 &amp;&amp; sendNewMsg.status == 200){
                          //Clear out the existing timer to avoid multiple timer instances running before chat box update.
                          clearInterval(mTimer);
                          // Update chat box after message has been sent.
                          getChatText(recIdVar);
                      }
                  }// End: Undefined Callback for chat message sending
              }       
      }   
  }
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>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 * FROM `member_bio` WHERE 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 INTO `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);
  
  }
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;?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 * FROM `member_bio` where m_id = '$rec_id'");
  $result_row = @mysql_fetch_assoc($qry_result);
  $rec_name = $result_row['f_name']." ";
  $rec_name .= $result_row['l_name'];
  
  
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;&lt;?php echo $rec_name; ?&gt;&lt;/title&gt;
  
      &lt;script type="text/javascript" src="js/getchatmsg.js"&gt;&lt;/script&gt;
  
  &lt;style&gt;
  
  * {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;}
  
  
  &lt;/style&gt;
  
  &lt;/head&gt;
  
  &lt;body onLoad="javascript:allChatMsgLoader(&lt;?php echo $rec_id; ?&gt;);"&gt;
  
  &lt;div class="new_chat_main_holder"&gt;
  
  &lt;div id="new_chat_box" style="width:99%; height:290px; overflow: auto; background-color: #FC6; border: 1px solid #555555;"&gt;
  
  &lt;/div&gt;
  
  &lt;form id="frmmain" name="new_chat_msg_submit" onsubmit="return blockSubmit();"&gt;
          &lt;input type="text" id="new_message" name="new_message" placeholder="Type chat message here" autocomplete="off" maxlength="250" /&gt;
          &lt;input type="hidden" id="new_msg_sender" name="new_msg_sender" value="Dont know what to place here" /&gt;
          &lt;input type="hidden" id="new_msg_receiver" name="new_msg_receiver" value="Dont know what to place here" /&gt;
  &lt;/form&gt;
  
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Please note: im currently using a pop-up window for the private chat, stil working on a jquery dialog/modal box.</p>
  
  <p>Would be glad and grateful getting help.</p>