我正在开发的网站上有一个联系表,并且编写了一些 PHP,如果某些字段未正确填写,它将向页面返回错误,一切正常,如果填写正确,则发送电子邮件是不是正确的错误信息被发送出去和显示。
唯一的问题是,如果表单填写不正确,用户将被重定向到带有错误消息的页面,但表单位于页面下方,用户被重定向到的位置。我想是否有错误将用户重定向到同一页面,并回显错误消息,但在表单所在页面的下方。
我做了一些研究,发现我可以使用 id 锚点和标题,但如果我这样做,页面会加载到我告诉它的位置,但错误消息不会显示。我尝试使用带有 _GET 变量的 if 循环来检查它是否与错误消息中的某些内容匹配,但似乎无法让任何东西正常工作。
总结一下:
如果表单填写不正确,我希望用户返回到同一页面,但要降低表单所在的位置,并且代码中设置的错误消息变量要显示在表单上方.
我刚刚开始学习 PHP,所以我希望能得到一些帮助来解决这个问题。提前感谢您的任何帮助和建议。
以下是该网站的一些截图:http:
//imgur.com/a/Hv5OW
这是页面的代码:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "You must enter a name, email address and message";
}
if (!isset($error_message)) {
foreach ($_POST as $value) {
if (stripos($value, 'Content-Type:') !== false) {
$error_message = "There was a problem with the information you submitted";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your contact form submission has an error";
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$mail->ValidateAddress($email)) {
$error_message = "You must specify a valid email address";
}
if (!isset($error_message)) {
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom($email, $name);
$address = "name@email.co.uk";
$mail->AddAddress($address, "Corwen Forestry Timber Products Ltd.");
$mail->Subject = "Website Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if ($mail->Send()) {
header("Location: contact.html?status=thanks");
exit;
}
else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
;
?>
<?php
$title = "Corwen Forestry Online | Contact Us";
$active = "contact";
include("inc/header.php");
?>
<img class="banner" src="images/contact-us.jpg" alt="Contact us background image.">
<article class="band content-wrap clearfix">
<?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") { ?>
<section>
<p class="contact-thanks">Thanks for getting in touch, we'll get back to you as soon as we can.</p>
</section>
<?php }
else { ?>
<header>
<h2>Contact Us</h2>
</header>
<section class="contact-intro">
<p>
Lorizzle hizzle dolor fizzle amizzle, consectetuer adipiscing elizzle. Crackalackin sapien velizzle,
my shizz volutpizzle, suscipit quis, its fo rizzle vel, arcu. Pellentesque fizzle tortizzle. Izzle
mammasay mammasa mamma oo sa. Fusce izzle dolor dapibus turpis tempizzle tellivizzle. Crunk
pellentesque nibh izzle turpizzle. Fo shizzle my nizzle funky fresh crackalackin. Pellentesque
eleifend rhoncizzle nisi. In for sure habitasse platea dictumst. Donec dapibizzle. Curabitur gangsta
urna, pretizzle eu, pizzle ac, my shizz vitae, nunc. Da bomb suscipizzle. Integer break it down
pimpin' shiznit fo shizzle my nizzle.
</p>
</section>
<section id="error-anchor" class="contact-third first">
<img src="images/contact-icon-email.png" alt="email icon">
<h3><a href="mailto:sales@corwenforestry.co.uk">sales@corwenforestry.co.uk</a></h3>
</section>
<section class="contact-third">
<img src="images/contact-icon-address.png" alt="address icon">
<h3>
Ty’n Llidiart Industrial Estate<br>
Corwen<br>
Denbighshire<br>
LL21 9RZ
</h3>
</section>
<section class="contact-third">
<img src="images/contact-icon-phone.png" alt="phone icon">
<h3>01490 412 146</h3>
</section>
<?php
if (isset($error_message)) {
header("Location: contact.html#error-anchor");
echo '<p class="error-message">' . $error_message . '</p>';
}
;
?>
<form class="clearfix" method="post" action="contact.html">
<table class="contact-us">
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<p>Please leave the address field blank, it's an anti-spam measure.</p>
<input type="text" name="address" id="address">
</td>
</tr>
</table>
<input type="submit" value="Send Message">
</form>
<section class="map">
<iframe width="47.7%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"
src="https://maps.google.co.uk/maps/ms?msa=0&msid=208085933608816427574.0004e1242973ccf3517ee&ie=UTF8&t=m&ll=52.977589,-3.414001&spn=0.144707,0.291824&z=11&output=embed"></iframe>
<br/>
<small>View <a target="_blank"
href="https://maps.google.co.uk/maps/ms?msa=0&msid=208085933608816427574.0004e1242973ccf3517ee&ie=UTF8&t=m&ll=52.977589,-3.414001&spn=0.144707,0.291824&z=11&source=embed"
style="color:#0000FF;text-align:left">CFTP HQ and Mills</a> in a larger map
</small>
</section>
<section class="map-key">
<ul>
<li>
<img src="images/a-sml.png" alt="Head Office map icon key.">
Head Office & Shop
</li>
<li>
<img src="images/b-sml.png" alt="Gwyddelwern Sawmill map icon key.">
Gwyddelwern Sawmill
</li>
<li>
<img src="images/c-sml.png" alt="Llandrillo Sawmill map icon key.">
Llandrillo Sawmill
</li>
</ul>
</section>
<?php } ?>
</article><!-- end band content-wrap -->
<?php include("inc/footer.php"); ?>