Search

Building a Simple PHP Contact Form with Email Functionality

2 min read
2 views

In this tutorial, we will walk through the process of creating a basic contact form using PHP. We will cover the HTML structure, form validation, and the

Prompt
<!DOCTYPE html> <html> <head> <title>Contact Form</title> </head> <body> <h2>Contact Us</h2> <form method="POST" action="process_form.php"> <label for="name">Name:</label> <input type="text" name="name" id="name" required> <label for="email">Email:</label> <input type="email" name="email" id="email" required> <label for="message">Message:</label> <textarea name="message" id="message" required></textarea> <button type="submit">Submit</button> </form> </body> </html>

Prompt
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"]; // Validate form inputs (e.g., check if email is valid, required fields are not empty) if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // Send email $to = "your-email@example.com"; $subject = "New Contact Form Submission"; $body = "Name: " . $name . " Email: " . $email . " Message: " . $message; if (mail($to, $subject, $body)) { echo "Thank you for your message! We will get back to you soon."; } else { echo "Oops! Something went wrong. Please try again later."; } } else { echo "Please enter a valid email address."; } } ?>

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!