updated contact form

This commit is contained in:
Gary 2025-05-14 15:13:10 +01:00
parent f533f948a7
commit 75a68af31b

View File

@ -15,48 +15,51 @@ if (!isset($_SESSION['captcha'])) {
$_SESSION['captcha'] = [
'num1' => rand(1, 10),
'num2' => rand(1, 10),
'answer' => 0 // This will be calculated later
'answer' => $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2']
];
$_SESSION['captcha']['answer'] = $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2'];
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$number = trim($_POST['number']);
$phone = trim($_POST['phone']); // Assuming you have a phone input field
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$captcha_input = isset($_POST['captcha']) ? (int)$_POST['captcha'] : 0;
// Validate CAPTCHA
if ($captcha_input !== $_SESSION['captcha']['answer']) {
echo "<div class='alert alert-danger text-center'>Incorrect CAPTCHA answer. Please try again.</div>";
} else {
// Send the email
$to = "gary@uklb.co.uk"; // TEST EMAIL DESTINATION
$headers = "From: $email" . "\r\n" . "Reply-To: $email" . "\r\n" . "X-Mailer: PHP/" . phpversion();
$body = "You have received a new message from $name.\n\n" . "Subject: $subject\n\n" . "Message:\n$message";
if (mail($to, $subject, $body, $headers)) {
echo "<div class='alert alert-success text-center'>Thank you for contacting us. We will get back to you shortly.</div>";
} else {
echo "<div class='alert alert-danger text-center'>There was an error sending your message. Please try again.</div>";
$website_name = "Your Website Name"; // Replace with your actual website name
$from_email = "noreply@" . $_SERVER['HTTP_HOST']; // A common practice for website forms
$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 your website form:\n\n" .
"Name: " . $name . "\n" .
"Email: " . $email . "\n";
if (!empty($phone)) { // Only include phone number if it's provided
$body .= "Phone Number: " . $phone . "\n";
}
$body .= "Subject: " . $subject . "\n\n" .
"Message:\n" . $message;
// Generate a new CAPTCHA for the next submission
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>";
$_SESSION['captcha'] = [
'num1' => rand(1, 10),
'num2' => rand(1, 10),
'answer' => 0
'answer' => $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2']
];
$_SESSION['captcha']['answer'] = $_SESSION['captcha']['num1'] + $_SESSION['captcha']['num2'];
} else {
echo "<div class='alert alert-danger text-center'>There was an error sending your message. Please try again.</div>";
}
}
}
?>
<section class="ftco-section bg-light">
<div class="container">
<div class="row justify-content-center">
@ -66,35 +69,34 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
<div class="col-md-7 d-flex">
<div class="contact-wrap w-100 p-md-5 p-4">
<h3 class="mb-4">Get in touch</h3>
<form method="POST" id="contactForm">
<form method="POST" id="contactForm" class="contactForm">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name" placeholder="Name" required>
<input type="text" class="form-control" name="name" id="name" placeholder="Name">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Email" required>
<input type="email" class="form-control" name="email" id="email" placeholder="Email">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" name="number" id="number" placeholder="Phone Number" required>
<input type="text" class="form-control" name="phone" id="phone" placeholder="Phone Number (Optional)">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" required>
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea name="message" class="form-control" id="message" cols="30" rows="7" placeholder="Message" required></textarea>
<textarea name="message" class="form-control" id="message" cols="30" rows="7" placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<!-- Custom CAPTCHA -->
<div class="form-group">
<label for="captcha">Security Check: What is <?php echo $_SESSION['captcha']['num1']; ?> + <?php echo $_SESSION['captcha']['num2']; ?>?</label>
<input type="number" id="captcha" name="captcha" class="form-control" required>