PHP Complete Form Example
Certainly! A "complete form" in PHP typically involves creating a form using HTML and then processing the form data using PHP. Here's a simple example to illustrate the process:
Example: Simple Contact Form
1. Create the HTML Form
Create an HTML file named contact_form.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
</head>
<body>
<h1>Contact Us</h1>
<form action="process_form.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="4" cols="50" required></textarea><br><br>
<input type="submit" value="Send">
</form>
</body>
</html>
2. Process the Form Data
Create a PHP file named process_form.php to handle the form submission:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect and sanitize input data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format";
exit;
}
// Process the data (e.g., save to a database or send an email)
// For this example, we will just display the data
echo "<h1>Form Submission Successful</h1>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Message:</strong><br>$message</p>";
} else {
echo "Invalid request method.";
}
?>
Explanation:
-
HTML Form (
contact_form.html):- The
<form>tag specifiesaction="process_form.php"andmethod="post", which means the form data will be sent toprocess_form.phpusing the POST method. - Various input elements (
<input>and<textarea>) are used to collect user data. - The
requiredattribute ensures that the user must fill in the fields before submitting.
- The
-
PHP Processing (
process_form.php):$_SERVER["REQUEST_METHOD"]checks if the request method is POST.htmlspecialchars()is used to convert special characters to HTML entities, preventing XSS attacks.filter_var()withFILTER_VALIDATE_EMAILis used to validate the email format.- The script then outputs the submitted data or an error message if the email format is invalid.
Output Example:
After filling out the form and submitting it, you might see an output like this:
Form Submission Successful
Name: John Doe
Email: john.doe@example.com
Message:
Hello, I am interested in your services.
If the email format is incorrect, you will see:
Invalid email format
This is a basic example, but real-world scenarios might involve more complex processing, validation, and error handling.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
