Real-Time Domain Availability Checker in PHP (AJAX + WHOIS + DNS)
A modern PHP-based domain search tool with AJAX, DNS checks, and WHOIS fallback. Get instant domain availability results and smart suggestions in a fast, elegant UI.
Suggested:
Learn how to create a simple web messaging system using PHP and MySQL in this beginner-friendly tutorial. This step-by-step guide covers everything from setting up the project structure to connecting to the database, building the front-end form, and implementing dynamic message sending and fetching.
First, let's set up the project folder. Create a new directory for your project and structure it as follows:
/web_messaging_project
/assets
/css
/js
/includes
db.php
header.php
footer.php
/messages
index.php
send_message.php
fetch_messages.phpBefore we dive into the PHP code, you'll need a MySQL database to store the messages.
CREATE DATABASE web_messaging;3. Next, create a messages table to hold the chat data:
USE web_messaging;
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
sender VARCHAR(255),
receiver VARCHAR(255),
message TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Here, we have the following columns:
id: The unique identifier for each message.sender: The person who sent the message.receiver: The recipient of the message.message: The content of the message.timestamp: The time when the message was sent.Now let's write the PHP code to connect to the database. Create a file includes/db.php and add the following:
<?php
$host = 'localhost'; // Server address
$dbname = 'web_messaging'; // Database name
$username = 'root'; // Database username
$password = ''; // Database password (for XAMPP, it’s usually empty)
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>This file will be included wherever we need access to the database in our project.
Next, create the messaging interface where users can input their messages. Open the messages/index.php file and add the following HTML:
<?php include('../includes/db.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Messaging</title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<h1>Web Messaging</h1>
<div id="messages">
<!-- Messages will be dynamically loaded here -->
</div>
<form id="messageForm">
<input type="text" id="sender" placeholder="Your Name" required>
<input type="text" id="receiver" placeholder="Recipient's Name" required>
<textarea id="message" placeholder="Type your message here..." required></textarea>
<button type="submit">Send</button>
</form>
<script src="../assets/js/script.js"></script>
</body>
</html>
Create a PHP file send_message.php to handle the logic when a user submits a message. This will insert the message into the database.
<?php
include('../includes/db.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$message = $_POST['message'];
$stmt = $pdo->prepare("INSERT INTO messages (sender, receiver, message) VALUES (?, ?, ?)");
$stmt->execute([$sender, $receiver, $message]);
echo "Message sent successfully!";
}
?>Create another file fetch_messages.php to display the messages. We'll retrieve all messages from the database and display them in a list.
<?php
include('../includes/db.php');
$query = "SELECT * FROM messages ORDER BY timestamp DESC";
$stmt = $pdo->query($query);
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($messages as $message) {
echo "<p><strong>{$message['sender']} to {$message['receiver']}</strong>: {$message['message']} <em>({$message['timestamp']})</em></p>";
}
?>
To make the messaging experience smoother, we'll use JavaScript to handle form submission without reloading the page. Create a new file assets/js/script.js:
document.getElementById('messageForm').addEventListener('submit', function(event) {
event.preventDefault();
const sender = document.getElementById('sender').value;
const receiver = document.getElementById('receiver').value;
const message = document.getElementById('message').value;
const formData = new FormData();
formData.append('sender', sender);
formData.append('receiver', receiver);
formData.append('message', message);
fetch('send_message.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(data => {
document.getElementById('message').value = ''; // Clear the message field
loadMessages(); // Reload the messages
});
});
function loadMessages() {
fetch('fetch_messages.php')
.then(response => response.text())
.then(data => {
document.getElementById('messages').innerHTML = data;
});
}
// Initial load of messages
loadMessages();Finally, you can style your application with CSS. Create a file assets/css/style.css to make the interface more attractive:
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f4f4f4;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
#messages {
margin-bottom: 20px;
}
#messageForm input, #messageForm textarea {
width: 100%;
padding: 10px;
margin: 5px 0;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}With these steps, you've created a basic web messaging system using PHP, MySQL, and JavaScript. Users can send messages to each other, and the messages are displayed in real-time without refreshing the page. This simple messaging app lays the foundation for building more complex communication systems in the future. You can add more features, such as user authentication, message notifications, and more advanced real-time functionality with technologies like WebSockets.
Happy coding!
A modern PHP-based domain search tool with AJAX, DNS checks, and WHOIS fallback. Get instant domain availability results and smart suggestions in a fast, elegant UI.
Learn how to build an advanced AJAX contact form with PHP that sends email without page reload. Includes modern UI design, form validation, and secure email handling using PHP's mail() function.
Learn how to remove .php, .html, and .htm extensions from URLs using .htaccess. Create clean, SEO-friendly URLs on Apache servers with our step-by-step guide.