Creating a Contact Form
Build a contact form that sends messages via email.
Creating a Contact Form is a free PHP Academy lesson on CoddyKit — lesson 2 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Creating a Contact Form
Welcome to the lesson on creating a contact form! In this project, you’ll learn how to build a contact form that allows users to send messages via email. Let’s get started!

2
Setting Up the Project
Before you start, ensure you have:
- A web server like XAMPP or WAMP.
- A text editor or IDE (e.g., VS Code).
- A file named
contact.phpin your project directory. - PHP’s
mail()function enabled (required for sending emails).
Tip: For local testing, you may need to configure your mail server settings.
3
Creating the Basic HTML Form
Start by creating the HTML structure for your contact form. Example:
Key Point: Use the required attribute to ensure all fields are filled out before submission.
<!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="contact.php" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label><br>
<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" name="submit">Send Message</button>
</form>
</body>
</html>
4
Processing the Form Data
In your contact.php file, handle the form submission. Example:
Tip: Use htmlspecialchars() to prevent XSS attacks by escaping HTML characters.
<?php
if (isset($_POST['submit'])) {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
echo "Name: $name<br>";
echo "Email: $email<br>";
echo "Message: $message<br>";
}
?>
5
Validating the Input
Before processing, validate the input to ensure it meets your requirements. Example:
Key Point: Always validate user input to avoid invalid or malicious data.
<?php
if (isset($_POST['submit'])) {
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo "Invalid email address.";
exit;
}
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
echo "Valid input. Processing...";
}
?>
6
Sending the Email
Use PHP’s mail() function to send the form data to an email address. Example:
Tip: Use a valid From address to avoid email delivery issues.
<?php
if (isset($_POST['submit'])) {
$to = "admin@example.com"; // Replace with your email
$subject = "New Contact Form Submission";
$message = "Name: $name\nEmail: $email\nMessage:\n$message";
$headers = "From: $email";
if (mail($to, $subject, $message, $headers)) {
echo "Message sent successfully!";
} else {
echo "Failed to send the message.";
}
}
?>
7
Enhancing the Form
Here are some enhancements you can add:
- Display success or error messages in the form itself.
- Style the form using CSS for a better user interface.
- Implement CAPTCHA to prevent spam submissions.
Key Point: Enhancements improve user experience and security.
8
Debugging Email Issues
If the email is not sent, check the following:
- Ensure PHP’s
mail()function is enabled. - Verify the
Fromemail address is valid. - Check your server’s mail logs for errors.
Tip: Consider using a library like PHPMailer for advanced email features.
9
10
Great Job!
Congratulations! You’ve built a functional contact form in PHP. You learned how to create the form, process its data, validate input, and send emails. In the next lesson, we’ll build another fun project to enhance your PHP skills further. Let’s keep coding!

Frequently asked questions
Is the “Creating a Contact Form” lesson free?
Yes — the full text of “Creating a Contact Form” is free to read here on the web, and the PHP Academy course includes 2 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Creating a Contact Form”?
Build a contact form that sends messages via email. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 2, so you can start here or from the beginning and move at your own pace.
How long does the “Creating a Contact Form” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a Simple To-Do List
- Creating a Contact Form