Php 输入表单不';t分析错误并在提交时显示空白页
编辑:将PHP更改为推荐代码,仍然无法在页面上显示成功/错误 我正在尝试使此联系人页面正常工作,但当我单击“提交”按钮时,它似乎正在崩溃。我一单击,它就会将我重定向到/contact_form.php并显示一个空白页面。如果所有表格都正确填写,则消息将被发送。如果其中任何一个错误或未填写,则显示相同的空白页 HTML:Php 输入表单不';t分析错误并在提交时显示空白页,php,Php,编辑:将PHP更改为推荐代码,仍然无法在页面上显示成功/错误 我正在尝试使此联系人页面正常工作,但当我单击“提交”按钮时,它似乎正在崩溃。我一单击,它就会将我重定向到/contact_form.php并显示一个空白页面。如果所有表格都正确填写,则消息将被发送。如果其中任何一个错误或未填写,则显示相同的空白页 HTML: <form method="post" action="contact_form.php"> <div class="input-group-lg"&g
<form method="post" action="contact_form.php">
<div class="input-group-lg">
<label for="name">Your Name<i>*</i></label>
<input name="name" type="text" class="form-control" placeholder="What should I call you?">
</div>
<div class="input-group-lg">
<label for="email">Email Address<i>*</i></label>
<input name="email" type="email" class="form-control" placeholder="Email Address">
</div>
<div class="input-group-lg">
<label for="phone">Phone Number</label>
<input name="phone" type="text" class="form-control" placeholder="Phone Number">
</div>
<div class="input-group-lg">
<label for="message">Message<i>*</i></label>
<textarea name="message" class="form-control" placeholder="What's on your mind?"></textarea>
</div>
<div class="input-group-lg">
<label for="human">What is 2 + 2? (Anti-spam)<i>*</i></label>
<input name="human" type="text" class="form-control" placeholder="2 + 2 = ?">
</div>
<div class="text-center">
<?php echo $result; ?>
</div>
<div class="text-center">
<input name="submit" type="submit" value="Send Message" class="btn btn-custom"></input>
</div>
</form>
你的名字*
电子邮件地址*
电话号码
信息*
什么是2+2?(反垃圾邮件)*
PHP:
您的PHP文件中没有
print
或echo
语句
将
echo$result
添加到PHP文件的末尾以查看结果更改成功代码。这将在页面重新加载时打印成功或错误消息
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
if(!$errName&&!$errEmail&!$errMessage&&!$errHuman){
如果(邮件($to、$subject、$body、$from)){
打印“谢谢!我会联系的”;
退出();
}否则{
打印“抱歉,发送邮件时出错。请稍后再试。”;
退出();
}
}
确定是否设置了变量:
if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='í<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
if(isset($result)){
echo $result;}
if(!isset($errName)&&!isset($errEmail)&&!isset($errMessage)&!isset($errHuman)){
如果(邮件($to、$subject、$body、$from)){
$result='谢谢!我会与您联系';
}否则{
$result='抱歉,发送您的邮件时出错。请稍后再试。';
}
}
如果(isset($result)){
echo$result;}
试试这个。我还没有测试过,但这应该很管用。如果你仍然得到错误,请张贴他们
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example@gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
if(isset($\u POST['name']){//仅当设置了name时运行
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$human=intval($_POST['human']);
//丢失数据的错误处理
如果(!$name)|(!$email)|(!$phone)|(!$message)|($human!==4)){
$result='错误:您没有提交以下所需信息:';
如果(!$name){
$result.='请输入您的姓名;
}否则如果(!$_POST['email']| |!filter_var($_POST['email'],filter_VALIDATE_email)){
$result.='请输入有效的电子邮件地址;
}如果(!$phone){
$result.='请输入电话号码;
}如果(!$message){
$result.='请输入您的信息;
}如果($human!==4){
$result.='请更正您的添加内容;
}
}否则{//错误处理结束,处理表单并发送电子邮件
$from='from:example';
$to$example@gmail.com';
$subject='messagefromx';
$body=“From:$name\n E-Mail:$email\n Phone:$Phone\n Message:\n$Message”;
邮件($to、$subject、$body、$from);
打印“谢谢!我会联系的”;
退出();
}//重复检查后关闭else
}else{//如果表单未使用变量发布,请放置默认的空变量,以便不显示警告或错误
$name=“”;
$email=“”;
$phone=“”;
$message=“”;
$human=“”;
$result=“”;
打印“抱歉,发送邮件时出错。请稍后再试。”;
退出();
}
我们是否使用echo
打印出一些东西???我在PHP代码中看不到任何输出。除了空白页,你还指望什么?或者HTML和PHP连接在同一个文件中?(先是PHP,然后是HTML)将PHP代码末尾的if语句中的$result=
更改为打印
。看我的答案。把我的密码改成那个。现在,当它成功的时候,我会在新的页面上看到成功的消息,只有一行文字。失败时,我仍然得到一张空白页。如何解析错误消息以及如何使它们都显示在实际的html页面上?添加错误报告(E_ALL);ini设置(“显示错误”,1)代码>到php页面的顶部。在编写php时,我总是在构建时将其添加到顶部。它将显示您的所有错误。然后将错误粘贴到此处注意:第33行的/contact_form.php中的未定义变量:errName注意:第33行的/contact_form.php中的未定义变量:errEmail您的错误变量仅在错误存在时存在,sooif(!$\u POST['message']){$errMessage='请输入您的消息';}
应该是if(!$\u POST['message']){$errMessage='请输入您的消息';}或者{$errMessage='';}
设置变量,即使错误不存在。但它什么也没显示。在php中,变量总是需要等于某个值。这样设置所有错误。或者,您可以在页面顶部设置所有选项<代码>$errMessage=''$errEmail=''$errHuman=''$errName=''代码>此外,您的错误消息无法打印,因为如果存在错误,您的“如果”!errName等语句不起作用。我正在重建你的php代码,并将很快将其粘贴为新的答案。如果这对你有效,我可以告诉你如何轻松打扮这一点。
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example@gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}