提交时PHP表单不显示成功或错误消息
Posted
技术标签:
【中文标题】提交时PHP表单不显示成功或错误消息【英文标题】:PHP Form not showing Success or Error messages when submitted 【发布时间】:2021-06-27 14:14:30 【问题描述】:我在这里查看了很多帖子,但我无法解决这个问题。 我有一个 php 表单,虽然发送了电子邮件,但它没有显示任何按摩,我不知道我错过了什么。
在 PHP 中它提到了成功和失败,但没有提及该消息所在的位置?我之前设置了 PHP 表单,但这让我失望。
有人可以帮忙吗?
sendmail.php 代码
<?php
// Define some constants
define( "RECIPIENT_NAME", "Email" );
define( "RECIPIENT_EMAIL", "email@email.com" );
// Read the form values
$success = false;
$senderName = isset( $_POST['username'] ) ? preg_replace( "/[^\s\S\.\-\_\@a-zA-Z0-9]/", "", $_POST['username']) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
// If all values exist, send the email
if ( $senderName && $senderEmail && $message)
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . "";
$msgBody = " Email: ". $senderEmail . " Message: " . $message . "";
$success = mail( $recipient, $headers, $msgBody );
//Set Location After Successsfull Submission
header('Location: contact.html?message=Successfull');
else
//Set Location After Unsuccesssfull Submission
header('Location: contact.html?message=Failed');
?>
contact.html的HTML代码:
<!--Contact Section-->
<section class="contact-section">
<div class="auto-container">
<div class="info-container">
<div class="row clearfix">
</div>
</div>
<div class="contact-container">
<div class="row clearfix">
<!--Form-->
<div class="form-column col-lg-6 col-md-12 col-sm-12">
<div class="inner wow fadeInLeft" data-wow-delay="0ms" data-wow-duration="1500ms">
<div class="sec-title">
<h2>Send <strong>Your Message</strong></h2>
<div class="lower-text">Don’t Hesitate to Contact Us</div>
</div>
<div class="default-form contact-form">
<form method="post" id="contact-form" action="sendemail.php">
<div class="row clearfix">
<div class="form-group col-lg-6 col-md-6 col-sm-12">
<div class="field-label">Name</div>
<div class="field-inner">
<input type="text" name="username" placeholder="Your Name" required="" value="">
</div>
</div>
<div class="form-group col-lg-6 col-md-6 col-sm-12">
<div class="field-label">Email</div>
<div class="field-inner">
<input type="email" name="email" placeholder="Email Address" required="" value="">
</div>
</div>
<div class="form-group col-lg-12 col-md-12 col-sm-12">
<div class="field-label">Message</div>
<div class="field-inner">
<textarea name="message" placeholder="Write your message..." required=""></textarea>
</div>
</div>
<div class="form-group col-lg-12 col-md-12 col-sm-12">
<button type="submit" class="theme-btn btn-style-three"><span class="btn-title">Send Your Message</span></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
感谢任何可以提供帮助的人。 :)
【问题讨论】:
if ( $senderName && $senderEmail && $message)
在您的情况下总是正确的。将 isset
: "";
的结尾更改为 : false;
或者如果变量不等于空字符串,请检查您的 if 语句
如果你想显示在contact.html?message=Failed
上发送的message
,你需要一些PHP 来查看并读取$_GET
【参考方案1】:
在 contact.php 页面中,您要检查 URL 中的消息参数并编写此代码以显示消息
<?php if($_GET['message'] == 'Successfull')?>
// here you can add your message when Successfull like
<p> message has been sent Successfully </p>
<?php else ?>
// here you can add your message when Failed
<p> sorry cant sent message </p>
<?php ?>
希望这个回答对你有帮助
【讨论】:
谢谢你,这不应该放在PHP文件中吗?以上是关于提交时PHP表单不显示成功或错误消息的主要内容,如果未能解决你的问题,请参考以下文章