Sure! I'll rectify and organize the provided code to ensure that the mail is sent correctly and the modal displays the "Success: Mail sent successfully." message.
HTML:
Make sure you include necessary libraries for Bootstrap modal functionality:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enquiry Form</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<!-- Modal -->
<div class="modal fade" id="successModal" tabindex="-1" role="dialog" aria-labelledby="successModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="successModalLabel">Success</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Your message has been sent successfully!</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Enquiry Form -->
<form id="enquiryForm">
<input type="text" id="enquiryName" placeholder="Name" required>
<input type="text" id="phoneNumber" placeholder="Phone Number" required>
<input type="email" id="email" placeholder="Email" required>
<textarea id="message" placeholder="Message" required></textarea>
<button type="button" onclick="sendEnquiry(this, 'example@example.com')">Send</button>
</form>
<!-- Bootstrap and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
function sendEnquiry(ele, mailTo) {
ele.innerText = 'Sending...';
ele.disabled = true;
const name = document.getElementById('enquiryName').value.trim();
const phoneNumber = document.getElementById('phoneNumber').value.trim();
const email = document.getElementById('email').value.trim();
const message = document.getElementById('message').value.trim();
const data = {
mailTo: mailTo,
name: name,
phoneNumber: phoneNumber,
email: email,
message: message
};
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
const response = JSON.parse(this.responseText);
if (response.status === 'success') {
$('#successModal').modal('show');
document.getElementById('enquiryForm').reset();
} else {
alert(`Error: ${response.message}`);
}
} else {
alert('Error: Unable to send enquiry.');
}
ele.innerText = 'Send';
ele.disabled = false;
}
};
xhr.open('POST', '/sendEnquiry.php', true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(data));
}
</script>
</body>
</html>
PHP:
Make sure the PHP file (sendEnquiry.php) on your server side processes the request correctly:
<?php
header("Content-Type: application/json");
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['name'], $input['phoneNumber'], $input['email'], $input['message'], $input['mailTo'])) {
echo json_encode(['status' => 'error', 'message' => 'Invalid input']);
exit;
}
$name = htmlspecialchars(trim($input['name']));
$phone = htmlspecialchars(trim($input['phoneNumber']));
$email = htmlspecialchars(trim($input['email']));
$message = htmlspecialchars(trim($input['message']));
$mailto = htmlspecialchars(trim($input['mailTo']));
if (!preg_match("/^\d{10}$/", $phone)) {
echo json_encode(['status' => 'error', 'message' => 'Invalid phone number. Please enter a 10-digit phone number.']);
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['status' => 'error', 'message' => 'Invalid email address.']);
exit;
}
$subject = "Contact Form Submission from $name";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$body = "Name: $name\nEmail: $email\nPhone: $phone\nMessage:\n$message";
if (mail($mailto, $subject, $body, $headers)) {
echo json_encode(['status' => 'success']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to send email. Please try again later.']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
}
?>
Explanation:
-
HTML & JavaScript:
- Uses Bootstrap CSS and JS for the modal.
- Modifies the button text to indicate the sending state.
- Uses XMLHttpRequest to perform a POST request to
sendEnquiry.php.
- Displays the modal on successful send.
-
PHP:
- Receives and decodes JSON input.
- Validates the inputs including phone number and email.
- Sends the email using the
mail() function.
- Returns JSON response indicating success or errors.
This setup ensures proper functioning of the email sending mechanism and displays the success modal when the email is sent successfully. Make sure that your server has the necessary configurations to send emails using the mail() function.