[英]PHP form submitting but not showing errors or success message until the form is reloaded
我有一个联系表单,其中使用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">×</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>
编辑:给定新信息,当您单击“提交”时,表单也会消失,您很可能会收到PHP错误。 请阅读有关PHP死亡白屏的信息,并启用错误消息传递功能,以便您进行故障排除并查看可能存在的错误。 我可能还会建议您研究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">×</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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.