In this tutorial, you will learn to create simple PHP Chat
Application using Jquery. This would be perfect for a live support
system for your website.
Step 2: Create a Chatting form
Step 3: Create one table to store the converstation
Step 4: Write AJAX – Jquery request script
Step 5: Write insert query script
Step 6: Display data from table using AJAX
index.php
Please comment your opinion about PHP Chat Application below and read our other PHP Blogs
How PHP Chat Application will work:
Step 1: Create a Login formStep 2: Create a Chatting form
Step 3: Create one table to store the converstation
Step 4: Write AJAX – Jquery request script
Step 5: Write insert query script
Step 6: Display data from table using AJAX
index.php
<!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>Chat Login || Chat System By Ravi Vadhel</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="login.php">
<label>Enter nickname:
<input type="text" name="user" id="user" maxlength="20" required title="Enter your username"/>
</label>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
</body>
</html>
login.php //to add new user into CHAT
<?php
// variables
$user = $_POST['user'];
$ip = $_SERVER['REMOTE_ADDR'];
// escape any malicious characters
if (!get_magic_quotes_gpc())
{
$user = addslashes($user);
}
if (empty($user))
{
echo "Please enter username.";
}
else
{
# connect to the database
include_once('connection.php');
// database query
$sql = mysql_query("INSERT INTO chat VALUES ('$user','joins the chat!',NULL,'$ip')");
mysql_close($link);
// create cookie and redierct user to chat page
setcookie("name",$user,time()+3600,"/");
header("Location:chat.php");
}
?>
chat.php
<html>
<head>
<title>Chat || Chat System By Ravi Vadhel</title>
</head>
<body>
<iframe src="messages.php" height="85%" width="100%" frameborder="0" scrolling="yes" title="All Messages">Sorry your neeed a browser upgrade to use this service. <br/>Please download <a href="http://www.mozilla.com/en-US/firefox/">firefox.com</a></iframe>
<br>
<br>
<form name="form1" method="post" action="send.php" onsubmit=" return send_message()">
<center>
<label><span class="style1">Type message and press ENTER on your KEYBOARD: </span>
<input name="message" type="text" id="message" size="150" title="Please type your message here!" autofocus required>
</label>
<input type="submit" name="Submit" value="Send Msg">
</center>
</form>
</body>
</html>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function send_message()
{
var message = $("#message").val();
$.ajax({ type: "POST",
url: "send.php",
data: {message:message},
success : function(text)
{
$("#message").val('');
}
});
return false;
}
</script>
connection.php
<?php
// MySQL database connection file
$SERVER = "127.0.0.1"; // MySQL SERVER
$USER = "root"; // MySQL USER
$PASSWORD = ""; // MySQL PASSWORD
$link = @mysql_connect($SERVER,$USER,$PASSWORD);
$db = mysql_select_db("chat");
?>
message.php
<?php # connect to the database include_once('connection.php'); // database query $sql = mysql_query('SELECT * FROM chat ORDER BY time ASC'); # show database results while($row = mysql_fetch_array($sql)) { echo '<b>'.$row['0'].'</b>: '.stripslashes($row['1'])."<br/>"; } mysql_close($link); # REFRESH PAGE echo '<META HTTP-EQUIV="refresh" CONTENT="5">'; ?>
send.php
You can download the source of Chatting application using PHP from here.<?PHP // variables $name = $_COOKIE["name"]; $message = $_POST["message"]; $ip = $_SERVER['REMOTE_ADDR']; # checks that the user nickname is present if( empty($name) ) { exit('You must <a href="index.php">login</a> in to chat!'); } else# checks that the user message is present if( empty($message) ) { exit('You must type a message!'); } // escape any malicious characters if (!get_magic_quotes_gpc()) { $message = addslashes($message); } # connect to the database include_once('connection.php'); // database query $sql = mysql_query("INSERT INTO chat VALUES('$name', '$message',NULL,'$ip')"); // if message was sent then redirect user to the chat messages else display error if (mysql_affected_rows() == 1) { // PHP redirection //header("Location:chat.php"); } else { echo "Sorry, your message could not be sent!"; } mysql_close($link); ?>
Please comment your opinion about PHP Chat Application below and read our other PHP Blogs
A great idea is to bundle-up and cover your nose and mouth so that the air you breathe is warm. That can help you avoid allergy attacks in the winter.
ReplyDeleteplay bazaar satta king