尝试删除电子邮件形式的PHP重定向功能并将其转换为弹出消息


Trying to remove email form PHP redirect function and turn it into a pop-up message

我正在使用下面的代码向我的地址发送电子邮件,提醒我有新用户注册:

<?php
$errors  = '';
$myemail = 'name@mydomain.com';
if (empty($_POST['email']) || empty($_POST['email']) || empty($_POST['email'])) {
    $errors .= "'n Error: all fields are required";
}
$name          = $_POST['name'];
$email_address = $_POST['email'];
$message       = $_POST['message'];
if (!preg_match(
      "/^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})$/i",
      $email_address
   )) {
    $errors .= "'n Error: Invalid email address";
}
if (empty($errors)) {
    $to            = $myemail;
    $email_subject = "Invitation Request: $email_address";
    $email_body    = "has received a new invitation request. " .
                     "'n Email: $email_address";
    $headers = "From: $myemail'n";
    $headers .= "Reply-To: $email_address";
    mail($to, $email_subject, $email_body, $headers);
    //redirect to the 'thank you' page
}
?>

目前它将用户重定向到一个新页面,但我希望它允许 javascript 弹出成功/失败消息,就像现代网站中使用的消息一样。

以下是消息代码(CSS 和 Javascript):

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<style type="text/css">
/* Status Message */
.message { 
    display: none; 
    margin: 0 0 13px 0; 
    padding: 13px 13px 13px 52px; 
    background: url('icon_check.gif') left no-repeat #EFA; 
    background-position: 13px 5px; 
    border: solid 1px #BD8; '
    width: 300px; 
}
</style>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
   $("#Button1").click(function(event){
     $(".message").show();
     $(".message").fadeOut(2500);
   });
 });
</script>

以下是成功消息的 HTML:

<div class="message">Your Invitation Request Has Been Received!</div>

可以这样做吗?在过去的两天里,我在几个地方寻找答案,但我似乎无法改变正确的事情。任何帮助将不胜感激。

使用当前的设置,执行此操作的最佳方法是使用查询字符串重定向回注册页面,该字符串告诉页面从那里打开窗口。

header("Location: /signup.php?success=true");

并在 JS 中

<script>
<?php
if(isset($_GET['success']) && $_GET['success'] == 'true'){
     echo "window.open( '/success.html', 'success', '[options],[..],[..]' );";
}
?>
</script>

另外,我建议使用filter_var来验证电子邮件。

filter_var('bob@example.com', FILTER_VALIDATE_EMAIL) 

最简单的方法是将PHP邮件功能与HTML页面相结合。您可以使用 AJAX 将发送邮件 PHP 页面加载到 DIV 中,这将用 PHP 函数的结果输出覆盖该 DIV 的内容。这将允许您为没有JS的用户提供回退,因为将表单提交到PHP页面仍然有效。