This project demonstrates how to create a simple web form using HTML and PHP, and retrieve the submitted data into a text document. It is a beginner-friendly solution ideal for understanding form handling and data storage with PHP.
-
User-Friendly Form:
- A simple HTML form to collect user input (e.g., name, email, and message).
-
Data Handling:
- Retrieve and process form data using PHP.
-
File Storage:
- Save the submitted data to a text file for record-keeping.
-
XAMPP Compatibility:
- Runs seamlessly on a local XAMPP server.
- Install XAMPP on your computer.
- Start the Apache server from the XAMPP control panel.
- Save the project files in the
htdocs
folder within the XAMPP installation directory.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic Web Form</title>
</head>
<body>
<h2>Submit Your Details</h2>
<form action="process.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="message">Message:</label><br>
<textarea id="message" name="message" rows="5" required></textarea><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
$data = "Name: $name\nEmail: $email\nMessage: $message\n---\n";
$file = 'submissions.txt';
if (file_put_contents($file, $data, FILE_APPEND)) {
echo "<p>Data saved successfully!</p>";
} else {
echo "<p>Failed to save data.</p>";
}
} else {
echo "<p>Invalid request method.</p>";
}
?>
-
Contact Forms:
- Collect user queries or feedback on a website.
-
Data Collection:
- Gather information from users for surveys or registrations.
-
Learning Project:
- A great starting point for understanding PHP and HTML integration.
-
Database Integration:
- Store form data in a MySQL database instead of a text file.
-
Validation & Security:
- Add server-side validation and protection against SQL injection.
-
Enhanced UI:
- Use CSS or frameworks like Bootstrap to style the form.
- Beginner-Friendly: Simple structure makes it easy to understand and extend.
- Reusable: The code can be adapted for various data collection needs.
- Practical Learning: Demonstrates a real-world use case of web forms and PHP.
This project provides a fundamental understanding of creating and processing web forms with HTML and PHP. By saving data into a text file, it introduces essential concepts of server-side scripting and file handling.
💡 Start building web forms and collecting data today!