Chat application is very common in web today. In this post i will explain you how to create a simple chat application using PHP, Ajax and MySql. Before we start creating Chat application try a live demo of the app which are we going to create.


The chat application we are building is very simple. It include a start form where user enter his email address and also enter his friend email which he want to chat and same thing done by his friend we use these email as unique and send their message to each other.


Step 1 : HTML Markup

First we create a start page {index.php} where user enter his email and his friend email address as shown in above image.

Step 2 : Chat Room

After filling all information in index page { index.php } we display a chat box to user to start chat with his friend.  I create a simple box where user type his message to his friend and friend reply.

i create a simple form to do that thing and use ajax to send and receive message between both.

Now i split this code to help you understand this .

First i get both email address and save that address to a variable. Now i create a simple html form to allow user to type his message and display that send message in chat box.

Note in above form i create two hidden fields where i echo both my email address{mid} and my friend email{fid}

to make conversation between both after that i create a simple javascript function to send and receive message from database. I use Jquery plugin to make thing work and also use livestamp plugin to display the time of message eg.{a second ago, a minute ago, 1 min, …..}


these function are the backbone of the whole chat system. I also handle enter button so whenever user type new message to friend and hit enter button message is send to database and also if user like to press submit button.

Step 3 : Backend for Chat

Now this is the most important part of chatroom where we can save data and send new message to user screen.

Database for Chat: 

I create a simple database for my chat where i save both email address, status of message and time of message.

Here status by default is set to 1 mean new message and when user read that message it change to .

Code to handle all chat room request: chatM.php

In above code i create my database connection and perform Insert and delete operation with php and return all result in json format. This is a simple chat room you can try live demo or download complete file from above link Thank you.

Posted in: php