php - 想要回显“已发送电子邮件!” 提交表单后在特定的 div 中
问题描述
我正在为我的网站实施联系表格。当我单击“提交”按钮时,它会向我发送电子邮件,然后将用户重定向到 domain.com/mail.php 并回显“电子邮件已发送!” 在空白的白页中。相反,我想让用户留在 index.html 中并让“电子邮件已发送!” 里面回荡着<div class="alert-msg"></div>
。任何帮助表示赞赏!
联系方式.php:
<?php
if(isset( $_POST['fname']))
$name = $_POST['fname'];
if(isset( $_POST['email']))
$email = $_POST['email'];
if(isset( $_POST['subject']))
$subject = $_POST['subject'];
if(isset( $_POST['message']))
$message = $_POST['message'];
$content="Name: $name \n Email: $email \n Subject: $subject \n Message: $message";
$recipient = "myemail@domain.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
echo "Email sent!";
?>
索引.html
<div class="contactcontainer">
<form action="contact.php" method="POST" id="contact-form">
<label for="fname">Full Name</label>
<input type="text" id="fname" name="fname" placeholder="Your name" required>
<label for="email">E-mail</label>
<input type="text" id="email" name="email" placeholder="E-mail" required>
<label for="email">Subject</label>
<input type="text" id="subject" name="subject" placeholder="Subject" required>
<label for="subject">Message</label>
<textarea id="message" name="message" placeholder="Your message" style="height:200px" required></textarea>
<button class="btn btn-outline-light text-uppercase" onclick="document.getElementById('contact-form').submit();" type="submit" name="submit" id="submit">submit</button>
<div class="alert-msg"></div>
</form>
</div>
页面点击提交后: 邮件已发送!在带有 URL 的空白页面中
解决方案
要使 alert-msg 起作用,PHP 需要位于 html 之上。
这有很多问题,因为您没有对发布的数据进行清理,从而使您很容易受到攻击。
以下是对 PHP 的一些建议;
<?php
// You should santize input, examples below;
// $data = strip_tags($data); - this removes tags like <a>
// $data = trim($data); - this removes whitespace
// New code
$status = ""; // First set the alert-msg to a null/empty value so it's not shown before the form is submitted.
// Listen for when the submit button is clicked
if (isset($_POST['submit'])) {
$name = $_POST['fname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Now we have the post data and it has been sanitized we can check to make sure it isn't null/empty
if ($name == "" || $email == "" || $subject == "" || $message == "") {
// If any of these values are empty send the alert that email hasn't been sent
$status = "Email not sent!";
// If all values have some data, continue...
} else {
$content = "Name: $name \n Email: $email \n Subject: $subject \n Message: $message";
$recipient = "myemail@domain.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
// Set alert-msg value for success
$status = "Email sent!";
}
}
?>
通过删除该操作来更改 HTML 表单,并将您的 PHP 脚本包含在同一页面上的 HTML 之上。使用提交按钮,删除 onclick 部分,因为您使用 PHP 直接发送表单并将 php 值添加到 alert-msg div;
新的 HTML
<form method="POST" id="contact-form">
<button class="btn btn-outline-light text-uppercase" type="submit" name="submit" id="submit">submit</button>
</form>
<div class="alert-msg"><?php echo $status; ?></div>
我省略了您发布的其余 HTML,因为没关系。
推荐阅读
- c - 如何在 C 中将单词作为变量?
- python - 用 Python 理解链表
- postgresql - 为什么在创建 PostgreSQL 函数时出现语法错误?
- java - 在线程执行之间调用 join
- google-apps-script - 这个基于字段提交创建覆盖的附加组件缺少什么
- spring-boot - http://localhost:8080/v2/api-docs 给出 ","additionalProperties":{"type":"object"}}},"401":
- node.js - 文件选择器有问题。文件选择器处理不适用于同一页面的多个连接
- ansible - 在ansible中打印子字符串的匹配数
- google-sheets-formula - 如何摆脱工作表中丑陋的 div/0 错误
- angular - NullInjectorError:迁移到 ionic 4 时没有平台提供程序