Code For How To Create A Simple Web Based Chat Application
Code For How To Create A Simple Web Based Chat Application
<html lang="en">
<head>
</head>
<body>
<div id="wrapper">
<div id="menu">
</div>
<div id="chatbox"></div>
<form name="message" action="">
</form>
</div>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function () {});
</script>
</body>
</html>
margin: 0;
padding: 0;
body {
font-family: "Lato";
font-weight: 300;
}
form {
display: flex;
gap: 10px;
justify-content: center;
form label {
font-size: 1.5rem;
font-weight: bold;
input {
font-family: "Lato";
a{
color: #0000ff;
text-decoration: none;
a:hover {
text-decoration: underline;
}
#wrapper,
#loginform {
margin: 0 auto;
padding-bottom: 25px;
background: #eee;
width: 600px;
max-width: 100%;
border-radius: 4px;
#loginform {
padding-top: 18px;
text-align: center;
#loginform p {
font-size: 1.4rem;
font-weight: bold;
#chatbox {
text-align: left;
margin: 0 auto;
margin-bottom: 25px;
padding: 10px;
background: #fff;
height: 300px;
width: 530px;
overflow: auto;
border-radius: 4px;
#usermsg {
flex: 1;
border-radius: 4px;
#name {
border-radius: 4px;
#submitmsg,
#enter{
background: #ff9800;
color: white;
font-weight: bold;
border-radius: 4px;
.error {
color: #ff0000;
#menu {
display: flex;
#menu p.welcome {
flex: 1;
a#exit {
color: white;
background: #c62828;
padding: 4px 8px;
border-radius: 4px;
font-weight: bold;
.msgln {
margin: 0 0 5px 0;
.msgln span.left-info {
color: orangered;
.msgln span.chat-time {
color: #666;
font-size: 60%;
vertical-align: super;
font-weight: bold;
background: #546e7a;
color: white;
font-size: 90%;
border-radius: 4px;
margin: 0 5px 0 0;
.msgln b.user-name-left {
background: orangered;
session_start();
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
else{
function loginForm(){
echo'
<div id="loginform">
</form>
</div>
';
?>
if(!isset($_SESSION['name'])){
loginForm();
else{
?>
<div id="wrapper">
<div id="menu">
</div>
<div id="chatbox">
<?php
$contents = file_get_contents("log.html");
echo $contents;
?>
</div>
</form>
</div>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
});
</script>
<?php
?>
Step 4: Handling User Input
//If user submits the form
$("#submitmsg").click(function () {
$("#usermsg").val("");
return false;
});
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
?>
echo $contents;
?></div>
function loadLog(){
$.ajax({
url: "log.html",
cache: false,
success: function(html){
},
});
Auto-Scrolling
//Load the file containing the chat log
function loadLog(){
$.ajax({
url: "log.html",
cache: false,
success: function(html){
},
});
Complete Code
Here is the code for index.php:
<?php
session_start();
if(isset($_GET['logout'])){
if(isset($_POST['enter'])){
if($_POST['name'] != ""){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
else{
function loginForm(){
echo
'<div id="loginform">
</form>
</div>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
if(!isset($_SESSION['name'])){
loginForm();
else {
?>
<div id="wrapper">
<div id="menu">
</div>
<div id="chatbox">
<?php
$contents = file_get_contents("log.html");
echo $contents;
?>
</div>
</form>
</div>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function () {
$("#submitmsg").click(function () {
$("#usermsg").val("");
return false;
});
function loadLog() {
$.ajax({
url: "log.html",
cache: false,
//Auto-scroll
});
$("#exit").click(function () {
var exit = confirm("Are you sure you want to end the session?");
if (exit == true) {
window.location = "index.php?logout=true";
}
});
});
</script>
</body>
</html>
<?php
?>
session_start();
if(isset($_SESSION['name'])){
$text = $_POST['text'];
?>