0

我正在开发的网站上有一个联系表,并且编写了一些 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&rsquo;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&amp;msid=208085933608816427574.0004e1242973ccf3517ee&amp;ie=UTF8&amp;t=m&amp;ll=52.977589,-3.414001&amp;spn=0.144707,0.291824&amp;z=11&amp;output=embed"></iframe>
                <br/>
                <small>View <a target="_blank"
                               href="https://maps.google.co.uk/maps/ms?msa=0&amp;msid=208085933608816427574.0004e1242973ccf3517ee&amp;ie=UTF8&amp;t=m&amp;ll=52.977589,-3.414001&amp;spn=0.144707,0.291824&amp;z=11&amp;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 &amp; 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"); ?>
4

3 回答 3

2

我相信你想要这样的东西。如果您发布的表单不正确,它应该将用户重定向到如下链接:

Header('Location: contact.html?error=' . $error . '#error-anchor');

为了让它发挥作用,你必须改变几件事;

您的表单需要一个 id

<form class="clearfix" method="post" action="contact.html" id="error-anchor">

为了保持您的 url 干净,您可能需要更改错误处理,例如更改:

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
}
于 2013-07-11T10:23:52.860 回答
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">

希望这可以帮助。干杯!

于 2013-07-11T10:24:02.730 回答
-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
于 2013-07-11T10:18:28.973 回答