Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views7 pages

PHP Simulation Assignment

This document describes a basic chat application built with jQuery and PHP. It includes 3 files: index.php contains the HTML and JavaScript code, send_message.php handles submitting new messages to the server, and get_messages.php returns messages to display. The app allows a user to type and submit messages, which are appended to the chat window. It polls every 2 seconds for new messages from another simulated user to demonstrate basic client-server interaction. However, it requires improvements for a fully-functional chat like real-time updates and user authentication.

Uploaded by

sivashriraj1955
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

PHP Simulation Assignment

This document describes a basic chat application built with jQuery and PHP. It includes 3 files: index.php contains the HTML and JavaScript code, send_message.php handles submitting new messages to the server, and get_messages.php returns messages to display. The app allows a user to type and submit messages, which are appended to the chat window. It polls every 2 seconds for new messages from another simulated user to demonstrate basic client-server interaction. However, it requires improvements for a fully-functional chat like real-time updates and user authentication.

Uploaded by

sivashriraj1955
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PHP SIMULATION ASSIGNMENT

BY, SHRI RAAM S


22BCA056
II BC A
CODING :
INDEX.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Chat</title>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
</head>
<body>
<h1>Chat</h1>
<div id="chat-messages"></div>
<form id="chat-form">
<input type="text" name="message">
<button type="submit">Send</button>
</form>

<script>
$(document).ready(function() {
$("#chat-form").submit(function(event) {
event.preventDefault();
var message = $(this).find("input[name='message']").val();
// Send the message to server-side script
$.ajax({
url: "send_message.php",
type: "POST",
data: { message: message },
success: function(data) {
$("#chat-messages").append(data);
$("#chat-form input[name='message']").val("");
}
});
});

// Polling for new messages (replace with real-time solution for better
experience)
setInterval(function() {
$.ajax({
url: "get_messages.php",
success: function(data) {
$("#chat-messages").html(data);
}
});
}, 2000); // Check for new messages every 2 seconds
});
</script>
</body>
</html>

SEND_MESSAGE.php
<?php
if (isset($_POST['message'])) {
$message = htmlspecialchars($_POST['message']); // Sanitize input
// Save the message to a database or any persistent storage (replace with your
logic)
echo "<p><b>You:</b> $message</p>";
}
?>

GET_MESSAGE.PHP
<?php
// Fetch messages from a database or any persistent storage (replace with your
logic)
// Here, we simply return a dummy message
echo "<p><b>User 2:</b> Hello!</p>";
?>

EXPLANATION

Basic Chat application with jQuery and PHP


Building a full-fledged chat application is complex and involves features like user
authentication, database management, and real-time updates. Here's a simplified
demonstration using jQuery and PHP to illustrate the concept:

1. Server-side (PHP):

Create a file named index.php with the following content:


HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Chat</title>
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
</head>
<body>
<h1>Chat</h1>
<div id="chat-messages"></div>
<form id="chat-form">
<input type="text" name="message">
<button type="submit">Send</button>
</form>

<script>
$(document).ready(function() {
$("#chat-form").submit(function(event) {
event.preventDefault();
var message = $(this).find("input[name='message']").val();
// Send the message to server-side script
$.ajax({
url: "send_message.php",
type: "POST",
data: { message: message },
success: function(data) {
$("#chat-messages").append(data);
$("#chat-form input[name='message']").val("");
}
});
});

// Polling for new messages (replace with real-time solution for better
experience)
setInterval(function() {
$.ajax({
url: "get_messages.php",
success: function(data) {
$("#chat-messages").html(data);
}
});
}, 2000); // Check for new messages every 2 seconds
});
</script>
</body>
</html>
Use code with caution.
Create another file named send_message.php with the following content:
PHP
<?php
if (isset($_POST['message'])) {
$message = htmlspecialchars($_POST['message']); // Sanitize input
// Save the message to a database or any persistent storage (replace with your
logic)
echo "<p><b>You:</b> $message</p>";
}
?>
Use code with caution.
Create a file named get_messages.php with the following content:
PHP
<?php
// Fetch messages from a database or any persistent storage (replace with your
logic)
// Here, we simply return a dummy message
echo "<p><b>User 2:</b> Hello!</p>";
?>
EXPLANATION:

The index.php file contains the HTML structure and the Javascript code using
jQuery.
The chat-form submits a message upon clicking the "Send" button.
The jQuery code intercepts the form submission, retrieves the message text, and
sends it to the server-side script "send_message.php" using AJAX.
The send_message.php script sanitizes the input and simulates saving it (replace
with actual database interaction). It then creates a message element with the
submitted text.
The success callback of the AJAX request appends the received message to the
chat-messages div.
Important Note: This example uses a simple polling mechanism to check for new
messages every 2 seconds. This is not ideal for real-time updates. You should
consider using WebSockets or Server-Sent Events for real-time communication.
3. Running the application:
Save the above files in a web directory.
Open index.php in your web browser.
Type a message in the input field and click "Send".
You will see your message and a pre-defined message ("Hello!") from another
user (simulated) appear in the chat window.
Remember: This is a very basic example and needs significant improvements for a
fully functional chat application. It lacks features like user authentication, real-
time updates, message history, and security measures. However, it demonstrates
the basic interaction between the client-side (jQuery) and server-side (PHP) for a
simple chat application.

You might also like