作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我想标题已经说明了一切,我有一个连接到单个输入表单的小 php 脚本。我想要的只是一个成功页面,在用户提交电子邮件后向其显示一个返回链接。这应该很容易,但是我对 php 还很陌生。
无论如何,这是 php 和 html 代码:
<?php
$visitor_email = $_POST['email'];
$email_from = "LightDesigns";
$email_subject = "Newsletter group";
$email_body = "You have received a new person to add to the newsletter:
$visitor_email.\n".
$to = "stefanvujic576@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
<section id="newsletter" style="margin-bottom: 0px;">
<div class="container">
<h1>Subscribe To My Newsletter</h1>
<form action="email.php" method="POST" name="newsletterForm">
<input class="emailBox_1" type="email" placeholder="Enter Email..."
name="email">
<button class="button_1" type="submit" class="button_1">
<span>Subscribe</span></button>
</form>
</div>
最佳答案
您只需要使用 mail() 结果并在 View 中添加一个 IF 语句。此外,使用 isset() 以便邮件不会在页面打开时触发,而只会在电子邮件提交时触发。
<?php
if (isset($_POST['email'])){
$visitor_email = $_POST['email'];
$email_from = "LightDesigns";
$email_subject = "Newsletter group";
$email_body = "You have received a new person to add to the newsletter:
$visitor_email.\n".
$to = "stefanvujic576@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$result = mail($to,$email_subject,$email_body,$headers);
// if ($result){
// Redirect to success
// header("location:success.php");
// exit();
// }
}
?>
<section id="newsletter" style="margin-bottom: 0px;">
<div class="container">
<h1>Subscribe To My Newsletter</h1>
<?php
if ($result){
// Inline success
echo "Success";
// or
// Include success
// include("success.php");
// or
// redirect to success
// echo "<meta http-equiv=\"refresh\" content=\"0;URL='success.php'\" />";
}else{
?>
<form action="email.php" method="POST" name="newsletterForm">
<input class="emailBox_1" type="email" placeholder="Enter Email..."
name="email">
<button class="button_1" type="submit" class="button_1">
<span>Subscribe</span></button>
</form>
<?php
}
?>
</div>
与其通过电子邮件向您发送要添加的电子邮件,为什么不直接将其添加到数据库中呢?
关于javascript - 如何在表单上执行 php 脚本后显示成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256212/
我是一名优秀的程序员,十分优秀!