【问题标题】:PHP form submitting but not showing errors or success message until the form is reloadedPHP 表单提交但在重新加载表单之前不显示错误或成功消息
【发布时间】:2018-04-25 23:48:04
【问题描述】:

我有一个联系表,我在其中使用 PHP 来完成服务器端的工作。

我遇到的问题是单击提交按钮时表单正在提交,但没有显示表单、错误消息或成功消息。

当我重新加载页面时,会出现表单和相应的消息。

我看不出任何明确的理由来说明为什么会如此有帮助!

<?php

// $error and $success variable set to blank 
$error = ""; 
$successMessage = "";

// check to see if any $_POST variables have been entered
if ($_POST) {

    // if $_POST variable ["email"] has no value
    if (!$_POST["email"]) {
        // $error variable to add comment advising that data is missing
        $error .= "An email address is required!!!!!!!!<br>";
    }

    if (!$_POST["subject"]) {
        $error .= "The subject field is required!!!!<br>";
    }

    if (!$_POST["content"]) {
        $error .= "The content field is required!!!!!<br>";
    }

    // IF $error variable is no longer empty 
    if ($error != "") {
        // $error variable to genereate html + $error 
        $error = '<div><p><strong>Whoops! There were error(s) in the form:</strong></p>' . $error . '</div>';
    } else {    
        $emailTo = "email@example.com";
        $subject = $_POST["subject"];
        $content = $_POST["content"]; 
        $headers = "From: ".$_POST["email"];
        // IF all email variables have had values assigned then $successMessage to display html sucess message
        if (mail($emailTo, $subject, $content, $headers)) {
        $successMessage = '<div>Thank you for your enquiry, we will get back to you shortly!</div>';
        // ELSE email variable(s) are missing a value - $error variable to display html error message
        } else {
        $error = '<div>Unfortuantely your enquiry could not be sent, please try again later</div>';
        }

    }

}

?>

<html>

<div id="contactOuter">
    <div id="contactInner">
            <a href="javascript:void(0);"><span id="close">&times;</span></a>
            <h1>Get in touch</h1>
            <div><? echo $error.$successMessage;?></div>
                <form method="post">
                    <label for="email">Email address:</label><br>
                    <input name="email" type="email"  placeholder="Enter email" id="email">
                    <label for="subject">Subject:</label>
                    <input name="subject" type="text" id="subject">
                    <label for="content">What would you like to ask us?</label><br>
                    <textarea name="content" rows="7" id="content"></textarea>
                    <button type="submit" id="submit">Submit</button>
                </form>
    </div>
</div>

</html>

【问题讨论】:

  • 您在&lt;html&gt; 标记之前输出错误消息。查看页面源,它​​们会在那里,但浏览器不知道如何处理它们,因为它们出现在 &lt;html&gt; 标记之前
  • 您似乎也没有&lt;body&gt; 标签。如果您遵循基本规则,HTML 效果最佳
  • 您没有为表单元素指定 ACTION 的任何特殊原因?
  • @RiggsFolly,错误在echoed 在打开&lt;html&gt; 标记之后。我同意&lt;body&gt; 的评论。而且,我们应该使用&lt;?php 而不是简写&lt;?。我在切换服务器时遇到了问题,我无法控制的新服务器没有处理任何以&lt;? 开头的 php,它必须是&lt;?php。我建议看看你是否进入if ($_POST) { 块。回声里面和外面的东西。最好使用if (isset($_POST["submit"])) { 并将name="submit" 放在您的按钮上。

标签: php html


【解决方案1】:

编辑: 鉴于当您单击提交时表单也会消失的新信息,您很可能会遇到 PHP 错误。请阅读PHP's white screen of death 并启用错误消息,以便您可以排除故障并查看您可能遇到的错误。我可能还建议查看Xdebug 或其他类似的工具,以便您可以单步执行代码以查看到底发生了什么。

有几个问题。首先,始终使用&lt;?php。这是从个人经验。有些服务器被设置为无法识别&lt;?,信不信由你。

其次,基于表单打印到屏幕时变量没有值的事实,我认为您没有进入条件块。尝试改用if (isset($_POST["submit"])) {,然后改用&lt;button type="submit" id="submit" name="submit"&gt;Submit&lt;/button&gt; - 注意:考虑到表单甚至没有显示在表单提交上的新信息,这可能不是这种情况,但仍然是一种很好的做法。

如 cmets 中所述,包括 &lt;body&gt; 标记。此外,请确保包含其他推荐和必需的标记(例如,&lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;)。

还建议在表单上使用action="file.php" 属性。

<?php

// $error and $success variable set to blank 
$error = ""; 
$successMessage = "";

// check to see if any $_POST variables have been entered
if (isset($_POST["submit"])) {

    // if $_POST variable ["email"] has no value
    if (!$_POST["email"]) {
        // $error variable to add comment advising that data is missing
        $error .= "An email address is required!!!!!!!!<br>";
    }

    if (!$_POST["subject"]) {
        $error .= "The subject field is required!!!!<br>";
    }

    if (!$_POST["content"]) {
        $error .= "The content field is required!!!!!<br>";
    }

    // IF $error variable is no longer empty 
    if ($error != "") {
        // $error variable to genereate html + $error 
        $error = '<div><p><strong>Whoops! There were error(s) in the form:</strong></p>' . $error . '</div>';
    } else {    
        $emailTo = "email@example.com";
        $subject = $_POST["subject"];
        $content = $_POST["content"]; 
        $headers = "From: ".$_POST["email"];
        // IF all email variables have had values assigned then $successMessage to display html sucess message
        if (mail($emailTo, $subject, $content, $headers)) {
        $successMessage = '<div>Thank you for your enquiry, we will get back to you shortly!</div>';
        // ELSE email variable(s) are missing a value - $error variable to display html error message
        } else {
        $error = '<div>Unfortuantely your enquiry could not be sent, please try again later</div>';
        }

    }

}

?>

<html>
</body>

<div id="contactOuter">
    <div id="contactInner">
            <a href="javascript:void(0);"><span id="close">&times;</span></a>
            <h1>Get in touch</h1>
            <div><?php echo $error.$successMessage;?></div>
                <form method="post">
                    <label for="email">Email address:</label><br>
                    <input name="email" type="email"  placeholder="Enter email" id="email">
                    <label for="subject">Subject:</label>
                    <input name="subject" type="text" id="subject">
                    <label for="content">What would you like to ask us?</label><br>
                    <textarea name="content" rows="7" id="content"></textarea>
                    <button type="submit" id="submit" name="submit">Submit</button>
                </form>
    </div>
</div>

</body>    
</html>

【讨论】:

  • 不幸的是,它似乎无法正常工作,并且确信它正在进入条件块。它仍然给我同样的问题,即单击提交按钮时表单消失,并且仅在我单击以再次打开表单时显示错误消息:(
  • 表格消失了?您必须关闭错误并且您收到 PHP 错误。 stackoverflow.com/questions/1475297/phps-white-screen-of-death
猜你喜欢
  • 2021-06-27
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2016-03-19
  • 1970-01-01
相关资源
最近更新 更多