title: Ajax Chatting ( Loading Messages ) repeated more than one
body: <p>I designed a chat room using ( Ajax , jquery , PHP , mysql )</p>
  
  <p>but when someone send a message to another the message repeated 4-5 times ,</p>
  
  <p>I'm using this code ( jquery) </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
            $(document).ready(function() {
              var interval = setInterval(function() {
                 $.ajax({
                    url: 'getChatting/182/212',
                    success: function(data) {
                      $('#messages').prepend(data);
                    } 
  
                });
               }, 1000); // ervery 1 second the message box refresh to loading new message
            });
        &lt;/script&gt;
  </code></pre>
  
  <p>// php code laravel</p>
  
  <pre><code>public function getChatting($from_id , $to_id){
      $ListChat = DB::table('cht_messages')
                  -&gt;where('user_from_id',$to_id)
                  -&gt;where('user_to_id',Auth::user()-&gt;id_user)
                  -&gt;where('readit_message',0)
  
                  -&gt;join('main_users','main_users.id_user','=','cht_messages.user_from_id')
                  -&gt;select('cht_messages.content_message','cht_messages.id_message' , 'main_users.display_name_user')
                  -&gt;orderby('cht_messages.created_at')
                  -&gt;get();
  
              DB::table('cht_messages')
              -&gt;where('user_from_id',$to_id)
              -&gt;where('user_to_id',Auth::user()-&gt;id_user)
              -&gt;update(array('readit_message'=&gt;1));
      $messages = '';
  
      if(Count($ListChat) &gt; 0):
          foreach($ListChat as $message) {
  
              $messages .= '&lt;span id="cht_auther" style="color:red;"&gt;'.$message-&gt;display_name_user.'&lt;/span&gt;
                  &lt;span id="cht_date"&gt;01-01-2014&lt;/span&gt;
                  &lt;span id="cht_spr"&gt; : &lt;/span&gt;
                  &lt;span id="cht_cnt"&gt;'.$message-&gt;content_message.'&lt;/span&gt;
                  &lt;br clear="both"/&gt;
  
                  ';
          }
      endif;
      return $messages;
  
  }
  </code></pre>
  
  <p>but it will repeate a message more than 6 or 7 , and php code it will take the last message which not read ...</p>
  
  <p>I'm sorry for my bad english</p>
  
  <p>Thanks</p>