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, andmessage. - The
formelement uses thePOSTmethod to send data tocontact.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
POSTmethod. - 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.comwith 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