Welcome back to our PHP tutorial series! In this article, we’ll walk through creating a simple contact form using PHP. Contact forms are essential for any website as they allow visitors to communicate with the site owner. We’ll cover how to create the HTML form, handle form submission with PHP, and validate the input. Let’s get started!
Step 1: Create the HTML Form
First, we need to create the HTML form that users will fill out. The form will include fields for the user’s name, email, subject, and message.
HTML Form Example
<!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>
<h2>Contact Us</h2>
<form action="contact.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="subject">Subject:</label>
<input type="text" id="subject" name="subject" required><br><br>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Step 2: Handle Form Submission with PHP
Next, we need to create the PHP script that will handle the form submission. This script will validate the input, send an email with the form data, and display a confirmation message.
PHP Script (contact.php)
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect and sanitize form input
$name = sanitize_input($_POST["name"]);
$email = sanitize_input($_POST["email"]);
$subject = sanitize_input($_POST["subject"]);
$message = sanitize_input($_POST["message"]);
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format";
exit;
}
// Prepare email
$to = "your-email@example.com"; // Replace with your email address
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$email_subject = "Contact Form: $subject";
$email_body = "You have received a new message from the contact form.\n\n".
"Name: $name\n".
"Email: $email\n".
"Subject: $subject\n".
"Message:\n$message";
// Send email
if (mail($to, $email_subject, $email_body, $headers)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message.";
}
}
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Step 3: Display Confirmation Message
When the form is submitted, the user should see a confirmation message indicating whether the message was sent successfully.
HTML Form with Confirmation Message
We’ll modify the HTML form to include a section for displaying the confirmation message.
<!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>
<h2>Contact Us</h2>
<form action="contact.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="subject">Subject:</label>
<input type="text" id="subject" name="subject" required><br><br>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea><br><br>
<input type="submit" value="Submit">
</form>
<div id="confirmation">
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Same PHP script as before
// Collect and sanitize form input
$name = sanitize_input($_POST["name"]);
$email = sanitize_input($_POST["email"]);
$subject = sanitize_input($_POST["subject"]);
$message = sanitize_input($_POST["message"]);
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format";
exit;
}
// Prepare email
$to = "your-email@example.com"; // Replace with your email address
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$email_subject = "Contact Form: $subject";
$email_body = "You have received a new message from the contact form.\n\n".
"Name: $name\n".
"Email: $email\n".
"Subject: $subject\n".
"Message:\n$message";
// Send email
if (mail($to, $email_subject, $email_body, $headers)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message.";
}
}
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
</div>
</body>
</html>
Conclusion
Creating a contact form using PHP involves creating an HTML form, handling form submissions with PHP, and validating user input. You can further customize and enhance this basic form to suit your needs, such as adding more fields, using a database to store messages, or implementing CAPTCHA for spam protection.
In our next article, we’ll explore PHP sessions, covering how to manage user sessions and store session data. Stay tuned and happy coding!
As always, if you have any questions or need further clarification, feel free to leave a comment below. We’re here to help you on your PHP journey!