首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在电子邮件消息发送后使php重定向?

如何在电子邮件消息发送后使php重定向?
EN

Stack Overflow用户
提问于 2017-06-19 18:29:14
回答 4查看 7.3K关注 0票数 0

因此,我试图在提交表单到主页后进行重定向。好吧,它是有效的,但有一个问题。当用户按下“提交”按钮时,消息不会显示,但会立即重定向到主页。我如何才能让用户首先收到“谢谢”消息,然后被重定向到主页?

代码语言:javascript
运行
复制
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
  <script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'contact_page.html';
  </script>
  <?php
}
else { ?>
    <script language="javascript" type="text/javascript">
      alert('Message failed. Please, send an email to gordon@template-help.com');
      window.location = 'contact_page.html';
    </script>
    <?php
}
header('Location: https://saprs.000webhostapp.com/index.html');
?>
EN

回答 4

Stack Overflow用户

发布于 2017-06-19 18:37:57

只需从您的代码中删除头文件并替换为以下内容

代码语言:javascript
运行
复制
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
 <script language="javascript" type="text/javascript">
  alert('Thank you for the message. We will contact you shortly.');
  window.location.href = 'index.html';
 </script>
 <?php
 }else { ?>
  <script language="javascript" type="text/javascript">
   alert('Message failed. Please, send an email to gordon@template-help.com');
   window.location.href = 'contact_page.html';
  </script>
 <?php } ?>
票数 2
EN

Stack Overflow用户

发布于 2017-06-19 19:04:46

请使用jQuery和ajax来实现。

HTML和jQuery:

代码语言:javascript
运行
复制
 <!DOCTYPE html>
    <html>
    <head>
        <title>Contact Form</title>
        <script src="//code.jquery.com/jquery-1.9.1.js"></script>
        <script type="text/javascript">
            function submitForm() {
             //Do validation and submit form
                $.ajax({
                  url: "mail.php",
                  type: "POST",               

                }).done(function( data ) {
                    alert(data);
                     if(data==1){
                         alert('Success');
                         window.location.href = 'test.php';//Your location 
                     }
                     else
                     {
                         alert('failed'); 
                     }
                });
                return false;
            }
        </script>
    </head>
    <body>
        <form method="post" id="mailData" name="mailData" onsubmit="return submitForm();">
            <label>Contact Form:</label><br>        
            <input type="submit" value="Submit" />
        </form>
        <div id="output"></div>
    </body>
</html>

服务器端PHP代码:

代码语言:javascript
运行
复制
<?php
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { 
   echo 1;
}
else { 
   echo 0;  
}

?>
票数 1
EN

Stack Overflow用户

发布于 2017-06-19 18:34:23

代码语言:javascript
运行
复制
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);
if($mail_status == true){
    echo "<script>alert('Thank you for the message. We will contact you shortly.');</script>";
    header('Location:https://saprs.000webhostapp.com/contact_page.html');
}else{
    echo "<script>alert('Sorry! Please try again.');</script>";
    header('Location:https://saprs.000webhostapp.com/contact_page.html');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44628146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档