How to Create a Contact Form Using PHP

How to Create a Contact Form Using PHP
Published in : 22 Nov 2024

How to Create a Contact Form Using PHP

A contact form is an essential feature for any website. It lets visitors reach out to you without exposing your email address to spam.If you’re wondering how to create a contact form using PHP, you’ve come to the right place.

In this guide, we’ll walk you through the entire process of building a simple and functional contact form using HTML and PHP. Whether you’re a beginner or brushing up on your skills, this tutorial has you covered.

What Is a Contact Form?

A contact form is a webpage feature that allows users to send messages directly to your email. It typically includes fields for:

  • Name
  • Email address
  • Subject or reason for contact
  • Message

By using PHP, you can process the submitted form data and send it to your email securely.

How to Create a Contact Form Using PHP

Let’s break the process into two parts: creating the front-end (HTML form) and processing the form with PHP.

Step 1: Create the HTML Form

Start by creating the form that users will see on your website. Here’s an 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><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="subject">Subject:</label><br>
        <input type="text" id="subject" name="subject" required><br><br>
        
        <label for="message">Message:</label><br>
        <textarea id="message" name="message" rows="5" required></textarea><br><br>
        
        <button type="submit">Send</button>
    </form>
</body>
</html>

Save this file as index.html. This form sends data to a file called contact.php for processing.

Step 2: Process the Form with PHP

Now, let’s create the contact.php file to handle the form submission and send the message via email.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $subject = htmlspecialchars($_POST['subject']);
    $message = htmlspecialchars($_POST['message']);
    
    $to = "your-email@example.com"; // Replace with your email address
    $headers = "From: $email\r\n";
    $emailBody = "Name: $name\nEmail: $email\n\nMessage:\n$message";
    
    if (mail($to, $subject, $emailBody, $headers)) {
        echo "Thank you! Your message has been sent.";
    } else {
        echo "Oops! Something went wrong. Please try again.";
    }
}
?>

Save this file as contact.php in the same directory as your HTML form.

Step 3: Test Your Contact Form

  1. Upload both files (index.html and contact.php) to your server.
  2. Open the index.html file in your browser.
  3. Fill out the form and click “Send.”
  4. Check your inbox for the email.

Tips for a Secure Contact Form

  • Validate Input: Ensure that all input fields are validated to prevent malicious data. Use htmlspecialchars() as shown above to sanitize data.
  • Use CAPTCHA: Add a CAPTCHA to protect your form from bots.
  • Implement SMTP: For reliable email delivery, use an SMTP server instead of PHP’s mail() function. Libraries like PHPMailer can simplify this process.

Conclusion Build Your Contact Form

Creating a contact form using PHP is a straightforward process that every website owner should know. With just a few lines of HTML and PHP, you can provide a professional way for visitors to connect with you.