How to Create a Simple Contact Form in PHP?

Alles, was PHP betrifft, kann hier besprochen werden.

How to Create a Simple Contact Form in PHP?

Postby joel2 » 27. July 2023 13:45

Being somewhat new to web programming, I want to use PHP to add a simple contact form to my website. I base my conclusions on Scalers' web development roadmap. Users should be able to enter their name, email address, and message on the form, and after submitting it, the information should be delivered to my email address.
I have some HTML and CSS knowledge, but I'm not sure how to proceed with the PHP part for handling the form submission and sending the email. Could someone please guide me on how to create this contact form using PHP? I'd appreciate a step-by-step explanation or some example code to get started.

Here's a simplified version of my HTML form:

Code: Select all
<!DOCTYPE html>
<html>
<head>
    <title>Contact Form</title>
    <!-- CSS and other head elements go here -->
</head>
<body>
    <h1>Contact Us</h1>
    <form method="post" action="contact_process.php">
        <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" rows="4" required></textarea>

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


Any help or example code for the PHP part would be greatly appreciated. Thank you!
joel2
 
Posts: 3
Joined: 26. April 2023 16:37
XAMPP version: 8.1.17
Operating System: Windows

Re: How to Create a Simple Contact Form in PHP?

Postby Nobbie » 27. July 2023 18:11

There are tons of PHP tutorials out there, we cannot teach PHP here.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: How to Create a Simple Contact Form in PHP?

Postby gulshan212 » 09. August 2023 17:31

Well, try below PHP Script for contact form.

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

$to = "your@email.com"; // Change to your email address
$subject = "New Contact Form Submission";
$headers = "From: $email";

mail($to, $subject, $message, $headers);

echo "Thank you for your message!";
}
?>

Thanks
User avatar
gulshan212
 
Posts: 20
Joined: 12. January 2023 12:14
Location: Haldwani, Uttarakhand
XAMPP version: 8.0.25
Operating System: Windows 10


Return to PHP

Who is online

Users browsing this forum: No registered users and 37 guests