Friday, November 3, 2017

XYZ Chat System Using PHP and MySQL


Communication is one of the most aspect of our human life as we are born in this world. We have learned how to communicate by gestures, hand signal and the language that is being taught to us by our parents.  There are thousands of languages and dialects all over the world that makes communication with other people more difficult and needs most of the time a translator just to translate one language to another language. One of the official language the is being spoke by millions of people around the world is the English language. The language itself is very universal the most text books and novels are being written using the English language.

Because of our shrinking world now a day’s communication becomes more easier compared to the past. I still remember during my early age my parents teach us how to write a letter and drop the letter to the post office. That’s the time the Internet is not widely used nor not yet existing in our country here in the Philippines.  By mid 1990’s the introduction of the Internet in our society paved way to a new method of communication the use of email, chat, text, voice over IP and video conferencing becomes a common medium of communication among us. It makes our communication to our friends, family members and relatives more efficient and best of all cost less. The interaction is in real time so there’s no need for us to wait for weeks or months just to receive the reply letter or message.

In this article I will teach you and discuss how to make a chat system using PHP and MySQL. When I have learned how to use mIRC, Yahoo Messenger and more recently chat system in Facebook I have also some questions in my mind how to create my own chat system. As I said before the use of Internet as source of information is very common now a days and very beneficial after some research I find some codes and try to improve and add functionality the is suited in my needs. That’s I came out with the program called XYZ Chat System 1.0 Using PHP and MySQL.  In writing the code I started with the use of HTML,CSS for the framework and layout of my page. After that I use JQuery  to refresh my page is every 1 second and finally the use of PHP and MySQL to save the messages in our database.

What the program will do is very simple I will ask the user to enter the name of the person below is a text field where all the conversation will be display to the end user and finally a text file where the user type the message and by clicking the send message it will display the message of the screen and save the message including the name of the sender in our MySQL database so we can monitor and retrieve whatever conversation that our user has in our system. This program is very simple there are still many things that can be done just to improve its overall functionality in general.

I test the chat system in my localhost it means in one computer and a LAN network. If you like to test this program in a LAN network my advice is for your mapped first the server computer and assign its IP address and for the of the client computer you also assigned a different IP address for example let say the server IP address is 192.168.20.10 when we access our program via the web browser we can use a command like this http://192.168.20.10/chat/index.php as simple with this command we can access of chat system that is being mapped in our windows operating system.

I am currently accepting programming work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.









Sample Program Output


Here is the complete program listing of XYZ Chat System 1.0 Using PHP and MySQL

index.php

<?php
session_start();
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("chat", $con);
                                $message=$_POST['message'];
                                $sender=$_POST['sender'];
                                mysql_query("INSERT INTO message(message, sender)VALUES('$message', '$sender')");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>XYZ Chat System 1.0</title>
<script language="javascript" src="jquery-1.2.6.min.js"></script>
<script language="javascript" src="jquery.timers-1.0.0.js"></script>
<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
                j(document).ready(function()
                {
                                j(".refresh").everyTime(1000,function(i){
                                                j.ajax({
                                                  url: "refresh.php",
                                                  cache: false,
                                                  success: function(html){
                                                                j(".refresh").html(html);
                                                  }
                                                })
                                })
                               
                });
               
   j('.refresh').css({color:"red"});
});
</script>
<style type="text/css">
.refresh {
    border: 1px solid black;
                border-left: 4px solid black;
    color: green;
    font-family: tahoma;
    font-size: 12px;
    height: 225px;
    overflow: auto;
    width: 400px;
                padding:10px;
                background-color:#FFFFFF;
}
#post_button{
                border: 1px solid #3366FF;
                background-color:#3366FF;
                width: 100px;
                color:#FFFFFF;
                font-weight: bold;
                margin-left: -105px; padding-top: 4px; padding-bottom: 4px;
                cursor:pointer;
}
#textb{
                border: 1px solid black;
                border-left: 4px solid black;
                width: 320px;
                margin-top: 10px; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; width: 415px;
}
#texta{
                border: 2px solid black;
                border-left: 4px solid black;
                width: 410px;
                margin-bottom: 10px;
                padding:5px;
}
p{
border-top: 1px solid #EEEEEE;
margin-top: 0px; margin-bottom: 5px; padding-top: 5px;
}
span{
                font-size:12;
                font-weight: bold;
                color: green;
}
</style>
</head>
<body bgcolor="lightgreen">
<font face="comic sans ms">
<h1> <center> XYZ Chat System 1.0 </center> </h1> </font>
<br>
<form method="POST" name="" action="">
<input name="sender" type="text" id="texta"  value="<?php echo $sender ?>"/>
<div class="refresh">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("chat", $con);
$result = mysql_query("SELECT * FROM message ORDER BY id DESC");
while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';
  }

mysql_close($con);
?>
</div>
<input name="message" type="text" id="textb"/>
<input name="submit" type="submit" value="Send Message" id="post_button" />
</form>
</body>
</html>

Here is the code that will refresh all the conversations of the user in chat system
refresh.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("chat", $con);
$result = mysql_query("SELECT * FROM message ORDER BY id DESC");
while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';
  }
mysql_close($con);
?>

chat.sql

-- phpMyAdmin SQL Dump
-- version 3.1.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 01, 2014 at 03:55 AM
-- Server version: 5.1.30
-- PHP Version: 5.2.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `chat`
--

-- --------------------------------------------------------

--
-- Table structure for table `message`
--

CREATE TABLE IF NOT EXISTS `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` text NOT NULL,
  `sender` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

--
-- Dumping data for table `message`
--

INSERT INTO `message` (`id`, `message`, `sender`) VALUES
(2, 'Welcome to our school Maria?', 'Mario'),
(3, 'Thank you Mario for the warm accommodation.', 'Maria'),
(4, 'What are your subject this semester?', 'Mario'),
(5, 'My subjects are Science, Math, English and Accounting.', 'Maria'),
(6, 'See you in school Maria God Bless you always.', 'Mario'),
(7, 'Thanks and Take care always.', 'Maria'),
(8, 'good bye maria', 'Mario'),
(9, 'Thank you Mario I hope we can me friends', 'Maria'),
(11, 'Good bye', 'Maria');



No comments:

Post a Comment