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); $num2 = rand(1, 10);
$captchaAnswer = $num1 + $num2; $captchaAnswer = $num1 + $num2;
if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
$name = trim($_POST['name']); $name = trim($_POST['name']);
$email = trim($_POST['email']); $email = trim($_POST['email']);
$phone = trim($_POST['phone']); $phone = trim($_POST['phone']);
$subject = trim($_POST['subject']); $subject = trim($_POST['subject']);
$message = trim($_POST['message']); $message = trim($_POST['message']);
$captcha_input = isset($_POST['captcha']) ? (int)$_POST['captcha'] : 0; $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']) { if ($captcha_input !== $submittedAnswer) {
echo "<div class='alert alert-danger text-center'>Incorrect CAPTCHA answer. Please try again.</div>"; $errorMessage = "Incorrect CAPTCHA answer. Please try again.";
// Store form data for re-population
$storedName = $name;
$storedEmail = $email;
$storedPhone = $phone;
$storedSubject = $subject;
$storedMessage = $message;
} else { } else {
$to = "enquiries@warmseal-roofing.co.uk"; $to = " enquiries@warmseal-roofing.co.uk";
$website_name = "Warmseal Roofing contact"; $website_name = "Warmseal contact";
$from_email = "noreply@" . $_SERVER['HTTP_HOST']; $from_email = "noreply@" . $_SERVER['HTTP_HOST'];
$headers = "From: " . $website_name . " <" . $from_email . ">" . "\r\n" . $headers = "From: " . $website_name . " <" . $from_email . ">" . "\r\n" .
"Reply-To: " . $name . " <" . $email . ">" . "\r\n" . "Reply-To: " . $name . " <" . $email . ">" . "\r\n" .
"X-Mailer: PHP/" . phpversion(); "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" . "Name: " . $name . "\n" .
"Email: " . $email . "\n"; "Email: " . $email . "\n";
if (!empty($phone)) { if (!empty($phone)) {
@ -42,20 +49,29 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
"Message:\n" . $message; "Message:\n" . $message;
if (mail($to, 'New Message from ' . $website_name, $body, $headers)) { 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 { } 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;
} }
} }
} else {
// Generate a new CAPTCHA for the next submission AFTER processing the form // Initialize stored data as empty on initial load
$_SESSION['captcha'] = [ $storedName = '';
'num1' => rand(1, 10), $storedEmail = '';
'num2' => rand(1, 10), $storedPhone = '';
'answer' => $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2'] $storedSubject = '';
]; $storedMessage = '';
$errorMessage = null;
} }
?> ?>
<section class="ftco-section bg-light padding-top-100 padding-bottom-100"> <section class="ftco-section bg-light padding-top-100 padding-bottom-100">