Home » » How to create a contact form for a website?

How to create a contact form for a website?

Creating a contact form for a website is an essential step in enabling visitors to communicate with you directly. Below is a guide on how to create a simple contact form using HTML, CSS, and PHP. The form will capture the user's name, email, subject, and message, and then send this information to a specified email address.

Step 1: HTML Structure

Start by creating the HTML structure for the contact form. Here’s a basic example:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Contact Us</title> <style> /* Basic CSS for styling the form */ body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; } .contact-form { max-width: 600px; margin: 50px auto; padding: 20px; background: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .contact-form h2 { margin-bottom: 15px; font-size: 24px; color: #333; } .contact-form input, .contact-form textarea { width: 100%; padding: 10px; margin: 5px 0 15px 0; border: 1px solid #ccc; border-radius: 3px; } .contact-form button { width: 100%; padding: 10px; background-color: #5cb85c; border: none; border-radius: 3px; color: #fff; font-size: 16px; } .contact-form button:hover { background-color: #4cae4c; } </style> </head> <body> <div class="contact-form"> <h2>Contact Us</h2> <form action="contact.php" method="POST"> <input type="text" name="name" placeholder="Your Name" required> <input type="email" name="email" placeholder="Your Email" required> <input type="text" name="subject" placeholder="Subject" required> <textarea name="message" placeholder="Your Message" rows="5" required></textarea> <button type="submit">Send Message</button> </form> </div> </body> </html>

Explanation:

  • The HTML structure includes basic form fields: name, email, subject, and message.
  • The form element uses the POST method to send data to contact.php.

Step 2: PHP Script for Processing the Form

Create a contact.php file to handle the form submission and send the email.

<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form data $name = htmlspecialchars(trim($_POST['name'])); $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL); $subject = htmlspecialchars(trim($_POST['subject'])); $message = htmlspecialchars(trim($_POST['message'])); // Check that data was sent to the mailer. if (!empty($name) && !empty($email) && !empty($subject) && !empty($message)) { // Recipient email address $to = "your-email@example.com"; // Replace with your email address // Email content $email_subject = "New Contact Form Submission: $subject"; $email_body = "You have received a new message from the contact form on your website.\n\n" . "Name: $name\n" . "Email: $email\n\n" . "Message:\n$message\n"; // Email headers $headers = "From: $name <$email>"; // Send the email if (mail($to, $email_subject, $email_body, $headers)) { echo "Message sent successfully!"; } else { echo "Failed to send the message!"; } } else { echo "All fields are required!"; } } else { echo "Invalid request method!"; } ?>

Explanation:

  • The script checks if the form is submitted using the POST method.
  • It sanitizes and validates the input data to prevent security risks.
  • It composes the email with the form data and sends it to the specified email address.
  • You should replace your-email@example.com with your actual email address where you want to receive the messages.

Step 3: Integrating and Testing

  • Place the HTML file and the PHP file on your web server.
  • Test the form by filling it out and submitting it.
  • Check your email to see if the form submission arrives as expected.

Additional Considerations

  • Spam Protection: Consider adding a CAPTCHA to prevent spam submissions.
  • Validation: Enhance the form with JavaScript validation for better user experience.
  • Security: Always sanitize inputs to prevent XSS and other security vulnerabilities.
  • Styling: Customize the CSS to match your website’s design.

By following these steps, you can create a functional and attractive contact form for your website.

0 comments:

Post a Comment

Office/Basic Computer Course

MS Word
MS Excel
MS PowerPoint
Bangla Typing, English Typing
Email and Internet

Duration: 2 months (4 days a week)
Sun+Mon+Tue+Wed

Course Fee: 4,500/-

Graphic Design Course

Adobe Photoshop
Adobe Illustrator

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 9,000/-

Web Design Course

HTML 5
CSS 3

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 8,500/-

Video Editing Course

Adobe Premiere Pro

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 12,000/-

Digital Marketing Course

Facebook, YouTube, Instagram, SEO, Google Ads, Email Marketing

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 15,000/-

Advanced Excel

VLOOKUP, HLOOKUP, Advanced Functions and many more...

Duration: 2 months (2 days a week)
Fri+Sat

Course Fee: 6,500/-

Class Time

Morning to Noon

1st Batch: 08:00-09:30 AM

2nd Batch: 09:30-11:00 AM

3rd Batch: 11:00-12:30 PM

4th Batch: 12:30-02:00 PM

Afternoon to Night

5th Batch: 04:00-05:30 PM

6th Batch: 05:30-07:00 PM

7th Batch: 07:00-08:30 PM

8th Batch: 08:30-10:00 PM

Contact:

Alamin Computer Training Center

796, Hazi Tower, West Kazipara Bus Stand,

West side of Metro Rail Pillar No. 288

Kazipara, Mirpur, Dhaka-1216

Mobile: 01785 474 006

Email: alamincomputer1216@gmail.com

Facebook: www.facebook.com/ac01785474006

Blog: alamincomputertc.blogspot.com

Contact form

Name

Email *

Message *