🚀 📩 Contact Form Project (HTML + PHP)
📁 Project Structure
contact-form/
│── index.html
│── process.php
│── success.php
│── style.css (optional)
🟢 1. HTML Form (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Us</title>
<style>
body { font-family: Arial; background:#f4f4f4; }
.container {
width: 400px;
margin: 50px auto;
background: #fff;
padding: 20px;
border-radius: 10px;
}
input, textarea {
width: 100%;
padding: 10px;
margin: 10px 0;
}
button {
background: #007bff;
color: white;
padding: 10px;
border: none;
}
</style>
</head><body><div class="container">
<h2>Contact Us</h2> <form action="process.php" method="POST">
<input type="text" name="name" placeholder="Enter Name" required>
<input type="email" name="email" placeholder="Enter Email" required>
<textarea name="message" placeholder="Enter Message" required></textarea>
<button type="submit">Send Message</button>
</form>
</div></body>
</html>
🟡 2. PHP Processing (process.php)
<?php
session_start();if ($_SERVER["REQUEST_METHOD"] == "POST") { // 🔹 Sanitization
$name = htmlspecialchars(trim($_POST['name']));
$email = htmlspecialchars(trim($_POST['email']));
$message = htmlspecialchars(trim($_POST['message'])); // 🔹 Validation
$errors = []; if (empty($name)) {
$errors[] = "Name is required";
} if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email";
} if (strlen($message) < 10) {
$errors[] = "Message must be at least 10 characters";
} // 🔹 If errors
if (!empty($errors)) {
$_SESSION['errors'] = $errors;
header("Location: index.html");
exit();
} // 🔹 Save to file (simple storage)
$data = "Name: $name\nEmail: $email\nMessage: $message\n----\n";
file_put_contents("messages.txt", $data, FILE_APPEND); // 🔹 (Optional) Send Email
// mail("your@email.com", "Contact Form", $message); $_SESSION['success'] = "Message sent successfully!";
header("Location: success.php");
exit();
}
?>
🟣 3. Success Page (success.php)
<?php
session_start();
?><!DOCTYPE html>
<html>
<head>
<title>Success</title>
</head>
<body><h2>
<?php
if (isset($_SESSION['success'])) {
echo $_SESSION['success'];
unset($_SESSION['success']);
}
?>
</h2><a href="index.html">Go Back</a></body>
</html>
🔴 4. (Optional) Show Errors on Form
Since index.html can’t read PHP sessions, rename it to:
👉 index.php
Then add this inside form page:
<?php
session_start();
if (isset($_SESSION['errors'])) {
foreach ($_SESSION['errors'] as $error) {
echo "<p style='color:red;'>$error</p>";
}
unset($_SESSION['errors']);
}
?>
🔐 Security Features Included
✔ POST method
✔ Input validation
✔ Sanitization (htmlspecialchars, trim)
✔ Email validation
✔ Session-based error handling
✔ Prevents XSS
✔ Basic data storage
🚀 Advanced Improvements (Highly Recommended)
You can upgrade this project:
✅ 1. Add CSRF Protection
$_SESSION['token'] = bin2hex(random_bytes(32));
✅ 2. Store Data in Database (MySQL)
Instead of messages.txt
✅ 3. Send Real Email (SMTP)
Use PHPMailer
✅ 4. Add Google reCAPTCHA
Prevent spam
✅ 5. Make Sticky Form
Keep user input after error
🎯 Final Result
You now have a:
- Fully working contact form
- Secure backend processing
- Error handling system
- Ready for real websites






