JustPaste.it

Your line here is related to my feedback:
    sql="INSERT INTO `chat`(`name`, `message`) VALUES ('".$name."', '".$msg."')";

You need to store both the sender and receiver but you're only storing at most one of those two.

Two people can have the same name so I wouldn't use that to identify the sender or receiver.  Use an autoincremented int id or a GUID for both sender and receiver.

Storing time that the message was sent would also help you determine what order the messages were sent in or what messages are newer than the last time the chat system requested them.

In other words, your insert statement should be more like:
        $sender_id = $_SESSION['user_id'];
    $sql="INSERT INTO `chat`(`sender_id`, `receiver_id`, `message`, ) VALUES ('".$sender_id."', '".$POST['receiver_id']."', '".$msg."')";

and your chat table needs to be updated to include the new columns.

The following could be used to store the date and time the message was sent:
created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP

I would make a few API's for:
- listing all messages sent to the currently authenticated user(based on session) or sent by the currently authenticated user after a specified UTC date time.  I would format the result using JSON so it is easily parsed by JavaScript.
- sending a message from currently authenticated user(based on session) to a posted receiver id.  You already started this with the script you shared.

I would then use a lot of JavaScript in your display part so that the sending and receiving works.  

The receiving could be handled with JavaScript that sends an HTTP request to fetch all relevant messages after the last requested time.  It could request a refresh every couple seconds.  The JavaScript could also relay those new messages to the appropriate UI elements.


For querying the relevant messages, you'd need to change like this:
$sql="SELECT * FROM `chat`";
to:
$user_id = _SESSION['user_id'];
$sql="SELECT * FROM `chat` where sender_id=".$user_id." or receiver_id=".$user_id;