We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. Learn More
contact@techquestworld.com
+919547614783
Premium Article
Published 3 months ago

How to Build a Real-Time Chat App in PHP (No JavaScript Needed)

5 min read
246 views
How to Build a Real-Time Chat App in PHP (No JavaScript Needed)
Photo by Techquestworld

Build a powerful real-time chat app using only PHP and AJAX – no JavaScript needed. Full source code and tutorial included.

1. Create MySQL Database

CREATE TABLE `messages` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(50) NOT NULL,
`message` TEXT NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2. HTML + AJAX Interface (index.php)

<!DOCTYPE html>
<html>
<head>
<title>PHP Chat App</title>
<style>
#chatbox { height: 300px; overflow-y: scroll; border: 1px solid #ccc; padding: 10px; }
</style>
</head>
<body>
<h2>Real-Time PHP Chat</h2>

<div id="chatbox"></div>

<form method="post" action="send.php">
<input type="text" name="username" placeholder="Your name" required>
<input type="text" name="message" placeholder="Your message" required>
<button type="submit">Send</button>
</form>

<script>
setInterval(function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'messages.php', true);
xhr.onload = function() {
document.getElementById('chatbox').innerHTML = this.responseText;
}
xhr.send();
}, 1000);
</script>
</body>
</html>

3. Handle Message Send (send.php)

<?php
$conn = new mysqli("localhost", "root", "", "chatapp");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $conn->real_escape_string($_POST['username']);
$message = $conn->real_escape_string($_POST['message']);
$conn->query("INSERT INTO messages (username, message) VALUES ('$username', '$message')");
}
header('Location: index.php');

4. Fetch Messages (messages.php)

<?php
$conn = new mysqli("localhost", "root", "", "chatapp");
$result = $conn->query("SELECT * FROM messages ORDER BY created_at DESC LIMIT 20");
while($row = $result->fetch_assoc()) {
echo "<p><strong>{$row['username']}:</strong> {$row['message']}</p>";
}

🔒 Need to secure your messages? Learn about PHP input sanitization here

In this blog post, we demonstrated how to build a real-time chat system in PHP without JavaScript, using AJAX (XMLHttpRequest) to simulate real-time behavior. We created a MySQL database to store messages, wrote PHP scripts to send and fetch messages and used minimal HTML and CSS to build the interface.


This type of project is beginner-friendly, works even on limited hosting (no Node.js or WebSockets) and teaches core concepts like:

• PHP-MySQL integration

• Simple AJAX requests using vanilla JS

• Securing form inputs

• Refresh-less page updates (via polling)


This post is highly recommended for:

• Students building PHP mini-projects

• Freelancers adding chat features to PHP websites

• Educators explaining AJAX and server-side interaction

Author
TAPAS SAHOO

Developer by Profession, Techie by Heart

A curious mind with a love for writing and technology, dedicated to simplifying web development and programming topics while keeping up with the ever-changing tech landscape.

Discussion (0)

Replying to
Previous Article
Top 10 Laravel Tricks Every Developer Regrets...

Boost your Laravel development speed and efficiency with these 10 must know hacks. Perfect for Larav...

Next Article
Speed Up Laravel Apps by 300% Using These Pow...

Learn how to boost your Laravel app speed by 300% with advanced caching techniques, including config...

Related Articles

Simple Contact Form in PHP with Email Functionality – Step-by-Step Guide
PHP
Simple Contact Form in PHP with Email Functionalit...

Build a working contact form using PHP that sends form data via email. A great beginner PHP project...

Modern PHP Newsletter Subscription Form with Stylish Design
PHP
Modern PHP Newsletter Subscription Form with Styli...

A minimal PHP-powered newsletter form to collect email addresses, designed with a clean, modern look...

Simple Feedback Form with PHP Email Support
PHP
Simple Feedback Form with PHP Email Support

Send feedback directly to your inbox using this beautifully designed PHP form.

Build a Complete PHP CRUD App with Bootstrap – No Framework
PHP
Build a Complete PHP CRUD App with Bootstrap – No...

Looking to build a full web app in PHP without relying on Laravel or CodeIgniter? This step-by-step...

Table of Contents