1

<?php
        if (isset($_GET['conv_id'])) {
            $conversation = $_GET['conv_id'];
            $select_conv = "select * from messages where conv_id='$conversation'";
            $run_conv = mysqli_query($conn, $select_conv);
            while ($row_conv = mysqli_fetch_array($run_conv)) {
                $message_text = $row_conv['msg_topic'];
                $message_user_1=$row_conv['sender'];
                $message_user_2=$row_conv['receiver'];
                if($message_user_1==$user_email){
                echo "<div class='col-md-7' style='box-shadow: 0 0 3px #101010;border-radius: 4px;margin-bottom: 10px ;padding: 20px;float: right; background-color: #eeeeee'>$message_text</div>";
            }
                elseif ($message_user_1==$user_id){
                    echo "<div class='col-md-7' style='box-shadow: 0 0 3px #101010;border-radius: 4px;margin-bottom: 10px ;padding: 20px;float: right; background-color: #eeeeee'>$message_text</div>";
                }
else{
    echo "<div class='col-md-7' style='box-shadow: 0 0 3px #101010;border-radius: 4px;margin-bottom: 10px ;padding: 20px;float: left;background-color: #67b168'>$message_text</div>";
}
   }          
   echo "<div class='col_md-12'>
<form action='' method='post''>
<div class=\"form-group\">
  <label for=\"comment\"></label>
  <textarea class=\"form-control\" rows=\"5\" id=\"comment\" name='message_content' required></textarea>
</div>
<button type='submit' class='btn btn-default' name='send_msg'>Send</button>
</form>
<br>
<br>
<br>
</div>";
}
?>
    </div>
        <?php
if(isset($_POST['send_msg'])){
    $conv_id=$_GET['conv_id'];
    $message_file=$_POST['message_content'];
    $user_sender=$row['user_id'];
    $insert_message="insert into messages(conv_id,msg_topic,msg_date,sender,receiver,status) VALUES ('$conversation','$message_file',NOW(),'$user_email','','unread')";
    $run_message=mysqli_query($conn,$insert_message);
    if($run_message){  
        echo "<script>window.open('mymessages.php?conv_id=$conv_id','_self')</script>";
    }
}
?>

I have a chat system with PHP working with to table with foreign key for relation between them Its work right but its slow because its need to reload page again and its not so pretty

I need an Ajax code or an Jquery or js code to do that with out reloading the page

something as like a socket programing

arash rahimi
  • 65
  • 12
  • try this url : http://www.vasplus.info/tutorial/88/chat-script-using-ajax-jquery-and-php---version-50 – Rax Shah Aug 06 '16 at 06:56
  • in this site after login u have able to download. if not then try this url :https://css-tricks.com/jquery-php-chat/ – Rax Shah Aug 06 '16 at 07:14

1 Answers1

1

AJAX and Sockets are two different things.

AJAX does not provide persistence connection, where as Sockets does. If you want to create chat system using Sockets in PHP, learn Websockets in PHP.

If you want to use AJAX to build Chat system, you may try Long polling which acts like persistence connection, but it's not as smooth and reliable as Sockets.

Sockets are more preferred then AJAX to bulid chat system.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
  • first thank you to answer me but I mean that I want to my chat system work as like as socket I know that sockets & Ajax are completely seperate – arash rahimi Aug 06 '16 at 07:03
  • I have a social network web application that one part of that is chat system, I have an other parts like : channel , group & user time line but its full php code and don't have js or Ajax – arash rahimi Aug 06 '16 at 07:09
  • You may try long polling if you want to make Chat system similar to Web sockets. This might help: http://stackoverflow.com/questions/333664/how-do-i-implement-basic-long-polling – Alok Patel Aug 06 '16 at 07:10