我正在开发一个有订阅部分的html页面 . 用户将他/她的电子邮件放在输入字段中并提交 . 然后将控件转移到php页面,该页面将订阅消息通过电子邮件发送给网站管理员,如果消息发送成功,则导航回index.html . 现在,此时我想显示订阅成功或失败的警告消息 . 如何在不创建index.html index.php的情况下这样做 .
<?php
$to = "@gmail.com";
$from = "edmin.media@gmail.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "Dear Admin!\nNew user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
if (mail($to, $subject, $body, $headers, "-f " . $from)) {
@header('location:index.html');
} else {
@header('location:index.html');
}
} else {
@header('location:index.html');
}
?>
1 回答
您可以在网址中传递GET变量,例如重定向到index.html?subscribed = yes并使用JavaScript检查它 .
见https://stackoverflow.com/a/901144/5063532
您还可以使用.htaccess文件将index.html重定向到php,以便用户看起来仍然在index.html上 .
见https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
但是,我不建议这样做,因为服务器端处理是首选,AJAX可能是理想的 .