Simple Contact Form

A contact form is an essential element for any website that wants to facilitate communication between the visitors and the site owners. In this tutorial, we will develop a simple contact form using HTML and PHP.

HTML Code

First, let's create the HTML structure for the contact form. Here’s a simple example:

<form action="process_form.php" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" required>
    
    <label for="message">Message:</label>
    <textarea id="message" name="message" required></textarea>
    
    <button type="submit">Send</button>
</form>

PHP Processing Script

Next, we will create a simple PHP script to handle the form submission. Save the following PHP code in a file named process_form.php:

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

// Email settings
    $to = 'youremail@example.com';
    $subject = 'New Contact Form Submission';
    $body = "Name: $name\nEmail: $email\nMessage: $message";
    $headers = "From: $name <$email>";

// Send email
    if (mail($to, $subject, $body, $headers)) {
        echo 'Message sent successfully!';
    } else {
        echo 'Error sending message.';
    }
}
?>

Conclusion

With this simple contact form, your website will be able to receive messages from visitors easily. Make sure to customize the PHP mail function settings according to your server configuration.