我正在开发的网站上有一个联系表单,如果某些字段没有正确填写,一切正常,电子邮件发送正确填写后,我会编写一些会将错误返回页面的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"); ?>
答案 0 :(得分:2)
我相信你想要这样的东西。如果您发布的表单不正确,则应将用户重定向到以下链接:
Header('Location: contact.html?error=' . $error . '#error-anchor');
为了实现这一目标,你将不得不改变一些事情;
您的表单需要ID
<form class="clearfix" method="post" action="contact.html" id="error-anchor">
要保持您的网址清洁,您可能需要更改错误处理,例如更改:
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "You must enter a name, email address and message";
}
到
if ($name == "" OR $email == "" OR $message == "") {
$error = 1; //to use in your redirect url
}
在您的页面顶部,您需要检查$_GET['error]
是否已设置(在重定向后使用)并相应地设置消息:
if ($_GET['error']) {
switch ($_GET['error']) {
case 1:
$error_message = "You must enter a name, email address and message";
break;
case 2:
$error_message = "Other error";
break;
}
} else if ($_POST) {
//form post handling
}
答案 1 :(得分:2)
当我从你的问题中得到你可以使用以下方法:
我们的想法是将表单“锚定”在“提交”上 - 这样我们就可以在表单完成成功或不成功通过后立即转到表单。
1)设置一个“锚” - 这样的动作:
<form class="clearfix" method="post" action="contact.html#myAnchor">
2)在表单上方设置一个“假”网址,如下所示:
<a href="#myAnchor"></a>
<form class="clearfix" method="post" action="contact.html#myAnchor">
你可以用自己喜欢的方式设置这个样式,或者在表单标签中使用id =“myAnchor”,如果它没有破坏任何东西。
3)使用您当前的代码,乍一看它看起来很好:
if (!isset($error_message)) {
// Send mail
} else {
// do everything you want with error variable.
}
4)回应表格上方的错误
<a href="#myAnchor"></a>
<?= $sFormattedErrorVariable ?>
<form class="clearfix" method="post" action="contact.html#myAnchor">
希望这会有所帮助。干杯!
答案 2 :(得分:-1)
您应该创建更智能的捕获错误。也许创建一个错误数组并添加新的错误。 有些人喜欢:
$error_message = array();
...
// $error_message[ID_MSG]
$error_message[2] = "text of msg";
...
<td>
<input <?php
if(isset($error_message[2])) {
echo 'value="' . $email . '" class="errorStyle"';
}
?> type="text" name="email" id="email">
</td>
...
对于计算错误,请使用以下内容:
count($error_message) > 0