To create a contact form, the front-end developers should follow these instructions:
- HTML Structure:
Start by creating an HTML form with the necessary input fields such as name, email, subject, and message. Also, include a CAPTCHA field to prevent spam submissions. Here's a basic structure to get started:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
</head>
<body>
<h1>Contact Us</h1>
<form id="contact-form" action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" required>
<br>
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50" required></textarea>
<br>
<img id="captcha-image" src="/captcha" onclick="refreshCaptcha()">
<br>
<label for="captcha">Enter CAPTCHA:</label>
<input type="text" id="captcha" name="captcha" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
- CSS Styling:
Apply CSS to style the form elements according to the desired design. You can use external stylesheets or inline styles. Here's a basic example of CSS to get started:
body {
font-family: Arial, sans-serif;
}
form {
width: 300px;
margin: 0 auto;
}
label {
display: block;
margin-top: 10px;
}
input[type="text"], input[type="email"], input[type="number"] {
width: 100%;
padding: 10px;
}
textarea {
width: 100%;
height: 100px;
padding: 10px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
- JavaScript Validation and Submission:
Add JavaScript to validate user input and submit the form data to a server-side script for processing. You can use the Fetch API or XMLHttpRequest for submitting the form data to the server. Here's an example of JavaScript to get started:
document.getElementById("contact-form").addEventListener("submit", function(event) {
event.preventDefault();
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const subject = document.getElementById("subject").value;
const message = document.getElementById("message").value;
const captcha = document.getElementById("captcha").value;
// Validate CAPTCHA
if (validateCaptcha(captcha)) {
submitForm(name, email, subject, message);
} else {
alert("Invalid CAPTCHA. Please try again.");
}
});
function validateCaptcha(captcha) {
// Implement server-side validation for CAPTCHA here
return true;
}
function submitForm(name, email, subject, message) {
const formData = {
name: name,
email: email,
subject: subject,
message: message
};
fetch("/submit", {
method: "POST",
headers: {
"Content-Type":