Fix: Scroll to form result after submission (PRG pattern with anchor)
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 23s
All checks were successful
Deploy Feature Branch to Test / deploy (push) Successful in 23s
This commit is contained in:
33
index.php
33
index.php
@@ -22,7 +22,20 @@ function containsHeaderInjection(string $value): bool
|
|||||||
// --- Form processing ---
|
// --- Form processing ---
|
||||||
$formErrors = [];
|
$formErrors = [];
|
||||||
$formSuccess = false;
|
$formSuccess = false;
|
||||||
|
if (!empty($_SESSION['form_success'])) {
|
||||||
|
$formSuccess = true;
|
||||||
|
unset($_SESSION['form_success']);
|
||||||
|
}
|
||||||
|
if (!empty($_SESSION['form_errors'])) {
|
||||||
|
$formErrors = $_SESSION['form_errors'];
|
||||||
|
unset($_SESSION['form_errors']);
|
||||||
|
}
|
||||||
|
if (!empty($_SESSION['form_data'])) {
|
||||||
|
$formData = $_SESSION['form_data'];
|
||||||
|
unset($_SESSION['form_data']);
|
||||||
|
} else {
|
||||||
$formData = ['fname' => '', 'lname' => '', 'email' => '', 'phone' => '', 'interest' => 'Besichtigung anfragen', 'message' => ''];
|
$formData = ['fname' => '', 'lname' => '', 'email' => '', 'phone' => '', 'interest' => 'Besichtigung anfragen', 'message' => ''];
|
||||||
|
}
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
// Collect and normalize input
|
// Collect and normalize input
|
||||||
@@ -37,8 +50,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$honeypot = normalizeContactValue((string) ($_POST['website'] ?? ''));
|
$honeypot = normalizeContactValue((string) ($_POST['website'] ?? ''));
|
||||||
if ($honeypot !== '') {
|
if ($honeypot !== '') {
|
||||||
// Bot detected – pretend success
|
// Bot detected – pretend success
|
||||||
$formSuccess = true;
|
header('Location: ' . $_SERVER['REQUEST_URI'] . '#form-result');
|
||||||
$formData = ['fname' => '', 'lname' => '', 'email' => '', 'phone' => '', 'interest' => 'Besichtigung anfragen', 'message' => ''];
|
$_SESSION['form_success'] = true;
|
||||||
|
exit;
|
||||||
} else {
|
} else {
|
||||||
// Server-side validation
|
// Server-side validation
|
||||||
if ($formData['fname'] === '') {
|
if ($formData['fname'] === '') {
|
||||||
@@ -91,14 +105,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$mailSent = mail($to, $subject, $body, $headers);
|
$mailSent = mail($to, $subject, $body, $headers);
|
||||||
|
|
||||||
if ($mailSent) {
|
if ($mailSent) {
|
||||||
$formSuccess = true;
|
|
||||||
$_SESSION['last_contact_submit'] = time();
|
$_SESSION['last_contact_submit'] = time();
|
||||||
$formData = ['fname' => '', 'lname' => '', 'email' => '', 'phone' => '', 'interest' => 'Besichtigung anfragen', 'message' => ''];
|
header('Location: ' . $_SERVER['REQUEST_URI'] . '#form-result');
|
||||||
|
$_SESSION['form_success'] = true;
|
||||||
|
exit;
|
||||||
} else {
|
} else {
|
||||||
$formErrors[] = 'Leider konnte die E-Mail nicht gesendet werden. Bitte versuchen Sie es später erneut oder schreiben Sie uns direkt an mki@kies-media.de.';
|
$formErrors[] = 'Leider konnte die E-Mail nicht gesendet werden. Bitte versuchen Sie es später erneut oder schreiben Sie uns direkt an mki@kies-media.de.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!empty($formErrors)) {
|
||||||
|
header('Location: ' . $_SERVER['REQUEST_URI'] . '#form-result');
|
||||||
|
$_SESSION['form_errors'] = $formErrors;
|
||||||
|
$_SESSION['form_data'] = $formData;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
@@ -635,14 +656,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
</p>
|
</p>
|
||||||
<div class="contact-form">
|
<div class="contact-form">
|
||||||
<?php if ($formSuccess): ?>
|
<?php if ($formSuccess): ?>
|
||||||
<div class="form-success" style="display: block">
|
<div id="form-result" class="form-success" style="display: block">
|
||||||
<p>Vielen Dank für Ihre Anfrage!</p>
|
<p>Vielen Dank für Ihre Anfrage!</p>
|
||||||
<br />
|
<br />
|
||||||
<small>Wir haben Ihre Nachricht erhalten und melden uns innerhalb von 24 Stunden bei Ihnen.</small>
|
<small>Wir haben Ihre Nachricht erhalten und melden uns innerhalb von 24 Stunden bei Ihnen.</small>
|
||||||
</div>
|
</div>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<?php if (!empty($formErrors)): ?>
|
<?php if (!empty($formErrors)): ?>
|
||||||
<div class="form-errors">
|
<div id="form-result" class="form-errors">
|
||||||
<ul>
|
<ul>
|
||||||
<?php foreach ($formErrors as $error): ?>
|
<?php foreach ($formErrors as $error): ?>
|
||||||
<li><?= escapeContactValue($error) ?></li>
|
<li><?= escapeContactValue($error) ?></li>
|
||||||
|
|||||||
Reference in New Issue
Block a user