Easy Creating a PHP Contact Form for html website

In today’s digital age, having a contact form on your website is essential for providing a seamless way for your visitors to get in touch with you. If you have a website built with HTML and are looking to add a contact form, PHP is a great choice for handling the form submission and processing the data. In this article, we will guide you through the process of creating a PHP contact form for your HTML website.

To get started, you will need to create a new PHP file on your server where you will handle the form submission. This file will contain the PHP code that will process the form data and send it to your email address. You can name this file something like “contact.php” for easy reference.

HTML Form (contact_form.html)

Contact Form

Contact Us

Next, you will need to create the HTML form on your website where users can input their information. You can include fields for their name, email address, subject, and message. Make sure to set the form action attribute to the URL of your “contact.php” file so that the form data is sent to the PHP file for processing.


// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
    echo "Please fill out all required fields.";

// Sanitize input fields
$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = htmlspecialchars($_POST['message']);

// Create email message
$to = "[email protected]";
$subject = "New Contact Form Submission";
$body = "Name: $name\nEmail: $email\nMessage:\n$message";

// Send email
if(mail($to, $subject, $body)) {
    echo "Your message has been sent successfully!";
} else {
    echo "Oops! Something went wrong, please try again later.";

In your “contact.php” file, you will need to use PHP code to retrieve the form data that was submitted and then send it to your email address. You can use the mail() function in PHP to send an email with the form data. Make sure to set the appropriate headers for the email, including the recipient email address, subject, and any additional headers you may need.

Finally, don’t forget to add validation to your form to ensure that users input the correct information. You can use PHP to check if the required fields are filled out and if the email address is in the correct format before sending the email.

By following these steps, you can easily create a PHP contact form for your HTML website. This will provide a professional and efficient way for your visitors to get in touch with you, ultimately improving the user experience on your website. So why wait? Start implementing a PHP contact form on your website today!

Leave a Reply

Your email address will not be published. Required fields are marked *

Exploring the Latest Web Design Trends of 2024