Simple PHP Contact Form: A 3-Step-by-Step Guide

Introduction

Having a functional contact form on your website is crucial for providing an easy way for your visitors to get in touch with you. Whether it’s for inquiries, feedback, or potential collaborations, a simple PHP contact form is a professional & user-friendly means of communication.

Feel free to learn how to build Online Quiz System in PHP in our previous post.

In this tutorial, we’ll walk you through creating a simple PHP contact form that you can integrate into your website.


Prerequisites

To follow along, you’ll need the following:

  1. A code editor (e.g., Visual Studio Code, Sublime Text). If you want to check out the best VS Code extensions.
  2. A local development environment with PHP (XAMPP, WAMP, or MAMP).
Prerequisite for simple PHP Contact Form
Prerequisite for simple PHP Contact Form

Step 1: Setting Up the HTML Form

Let’s start by creating the HTML structure for our contact form. In your code editor, create a new file and name it contact_form.php". Paste the following HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Contact Form</title>
</head>
<body>
    <h2>Contact Us</h2>
    <form action="process_form.php" method="post">
        <label for="name">Name:</label>
        <input type="text" name="name" required>

        <label for="email">Email:</label>
        <input type="email" name="email" required>

        <label for="message">Message:</label>
        <textarea name="message" required></textarea>

        <button type="submit">Submit</button>
    </form>
</body>
</html>

This code creates a basic HTML form with input fields for the user’s name, email, and message, along with a submit button. The form’s action attribute is set to “process_form.php” which is where we’ll handle form submissions using PHP.


Step 2: Creating the PHP Script

Now, let’s create the PHP script that will process the form submissions and send the email to your designated email address. In your code editor, create a new file named "process_form.php.

Paste the following PHP code:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    $to = "[email protected]"; // Replace this with your email address
    $subject = "New Contact Form Submission";
    $headers = "From: $email";

    // Send the email
    if (mail($to, $subject, $message, $headers)) {
        echo "Thank you! Your message has been sent.";
    } else {
        echo "Oops! Something went wrong. Please try again later.";
    }
}
?>

This PHP script checks if the form was submitted using the $_SERVER["REQUEST_METHOD"] variable. If the form was submitted via the “POST” method, it retrieves the form data (name, email, and message) using the $_POST array.

Replace "[email protected]" with your actual email address, where you want to receive the contact form submissions.


Step 3: Testing the Simple PHP Contact Form

To test our contact form, you must host it on a server that supports PHP.

If you are using a local development environment like XAMPP. copy both contact_form.php" and process_form.php files into the appropriate folder (i.e, htdocs).

  1. Launch your local server.
  2. In your web browser, go to http://localhost/contact_form.php (or the appropriate URL where you’ve hosted your files).

Fill out the simple HTML contact form using PHP with test data and click the “Submit” button. If everything is set up correctly, you should see a “Thank you! Your message has been sent.” message.

Customizing the Contact Form: Feel free to modify the HTML and CSS of the contact_form.php" file to match your website’s design. You can also add more fields or validation to the form as per your requirements.

Security Considerations: This tutorial covers the basics of a simple html contact form. In a real-website scenario, you must implement additional security measures to prevent misuse, such as:

  1. Input validation to ensure the data is in the correct format.
  2. Using CAPTCHA or other anti-spam techniques to prevent bots from submitting the form. You can try to free Google Recaptcha for the best bot security.
  3. Sanitizing and validating user inputs to prevent SQL injection and other attacks.
  4. Avoiding the use of the mail() function for critical communications due to its limitations.

Conclusion

In conclusion, creating a simple PHP contact form is a great starting point for establishing direct communication with website visitors.

With the simple email form HTML and the PHP script to process submissions. You can build upon this foundation to develop more sophisticated and secure contact forms as your needs grow. For example: submit your form with PHPmailer then check out this.

For fun, you can also check out the 25 best must-watch anime for relaxing.

Keep coding! Ta-da!

4 thoughts on “Simple PHP Contact Form: A 3-Step-by-Step Guide”

  1. Thanks for sharing your thoughts. I really appreciate your
    efforts and I am waiting for your next post thanks once again.

    Reply
  2. What’s Taking place i am new to this, I stumbled upon this I’ve discovered It
    positively helpful and it has aided me out loads.
    I hope to contribute & aid different customers like its helped
    me. Great job.

    Reply
  3. I believe this is one of the such a lot significant info for
    me. And i’m satisfied studying your article.
    But want to statement on some common issues, The web
    site taste is great, the articles is really nice : D. Just right job, cheers

    Reply
  4. Greetings! This is my first visit to your blog!
    We are a team of volunteers and starting a new initiative in a
    community in the same niche. Your blog provided us valuable
    information to work on. You have done a outstanding job!

    Reply

Leave a Comment