PHP表单提交但不显示错误或成功消息,直到重新加载表单

时间:2018-04-24 16:35:38

标签: php html

我有一个联系表单,我在其中使用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>

1 个答案:

答案 0 :(得分:0)

编辑:如果您点击提交时表单消失的新信息,您很可能会收到PHP错误。请阅读PHP's white screen of death 并启用错误消息,以便您排除故障并查看可能出现的错误。我可能还建议您查看Xdebug或其他类似工具,这样您就可以逐步查看代码,看看究竟发生了什么。

有一些问题。首先,始终使用<?php。这是来自个人经历。某些服务器设置为无法识别<?信不信任。

其次,基于表格打印到屏幕时变量没有价值的事实,我不认为你进入你的状态块。请尝试使用if (isset($_POST["submit"])) {,然后执行<button type="submit" id="submit" name="submit">Submit</button> - 注意:这可能不是这种情况,因为新的信息表单甚至没有显示在表单提交上,但仍然是良好做法。

如评论中所述,请添加<body>标记。另外,请确保包含其他推荐和必需的标记(例如<!DOCTYPE html><html lang="en">)。

还建议在表单上使用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>