testing new contact form code

This commit is contained in:
Gary 2025-06-17 11:53:55 +01:00
parent 34cd480bd5
commit 8f0dab9da5

View File

@ -15,24 +15,31 @@ $num1 = rand(1, 10);
$num2 = rand(1, 10);
$captchaAnswer = $num1 + $num2;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$captcha_input = isset($_POST['captcha']) ? (int)$_POST['captcha'] : 0;
$submittedAnswer = isset($_POST['captcha_answer']) ? (int)$_POST['captcha_answer'] : null;
if ($captcha_input !== $_SESSION['captcha']['answer']) {
echo "<div class='alert alert-danger text-center'>Incorrect CAPTCHA answer. Please try again.</div>";
if ($captcha_input !== $submittedAnswer) {
$errorMessage = "Incorrect CAPTCHA answer. Please try again.";
// Store form data for re-population
$storedName = $name;
$storedEmail = $email;
$storedPhone = $phone;
$storedSubject = $subject;
$storedMessage = $message;
} else {
$to = "enquiries@warmseal-roofing.co.uk";
$website_name = "Warmseal Roofing contact";
$to = " enquiries@warmseal-roofing.co.uk";
$website_name = "Warmseal contact";
$from_email = "noreply@" . $_SERVER['HTTP_HOST'];
$headers = "From: " . $website_name . " <" . $from_email . ">" . "\r\n" .
"Reply-To: " . $name . " <" . $email . ">" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$body = "You have received a new message from Warmseal Roofing Contact form:\n\n" .
$body = "You have received a new message from Bedford Wealth Contact form:\n\n" .
"Name: " . $name . "\n" .
"Email: " . $email . "\n";
if (!empty($phone)) {
@ -42,20 +49,29 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
"Message:\n" . $message;
if (mail($to, 'New Message from ' . $website_name, $body, $headers)) {
echo "<div class='alert alert-success text-center'>Thank you for contacting us. We will get back to you shortly.</div>";
header("Location: submission");
exit();
} else {
echo "<div class='alert alert-danger text-center'>There was an error sending your message. Please try again.</div>";
$errorMessage = "There was an error sending your message. Please try again.";
// Store form data for re-population
$storedName = $name;
$storedEmail = $email;
$storedPhone = $phone;
$storedSubject = $subject;
$storedMessage = $message;
}
}
// Generate a new CAPTCHA for the next submission AFTER processing the form
$_SESSION['captcha'] = [
'num1' => rand(1, 10),
'num2' => rand(1, 10),
'answer' => $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2']
];
} else {
// Initialize stored data as empty on initial load
$storedName = '';
$storedEmail = '';
$storedPhone = '';
$storedSubject = '';
$storedMessage = '';
$errorMessage = null;
}
?>
<section class="ftco-section bg-light padding-top-100 padding-bottom-100">