【问题标题】:How can I make php redirect after email message was sent?发送电子邮件后如何进行 php 重定向?
【发布时间】:2017-06-19 10:29:13
【问题描述】:

所以我在将表单提交到主页后尝试进行重定向。很好,它可以工作,但有一个问题。当用户按下“提交”按钮时,消息不会显示,但会立即重定向到主页。我怎样才能让用户首先收到“谢谢”消息,然后被重定向到主页?

<?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');
?>

【问题讨论】:

  • 把谢谢你,在你重定向到的页面上
  • 在回显/输出后不能使用 header()
  • 请注意,我对此完全陌生,所以我可能会搞砸很多事情。

标签: php redirect


【解决方案1】:

只需从代码中删除标题并替换为以下内容

$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 } ?>

【讨论】:

  • 当我使用这个时,我得到这个错误:解析错误:语法错误,意外'}',期望文件结束在 /storage/ssd2/729/2009729/public_html/contact.php 第 19 行
  • 您确定您的代码以&lt;?php 开头吗?如果没有,则在代码的最前面添加&lt;?php
  • 我添加了
  • 能否更新您的完整代码以便更好地理解?
【解决方案2】:

请使用 jQuery 和 ajax。

HTML 和 jQuery:

 <!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 代码:

<?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;  
}

?>

【讨论】:

    【解决方案3】:
    $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');
    }
    

    【讨论】:

      【解决方案4】:

      替换

      header('Location: https://saprs.000webhostapp.com/index.html');
      

      echo "<script type='text/javascript'>window.location.href='https://saprs.000webhostapp.com/index.html'</script>";
      

      【讨论】:

      • 是的,但这只是显示消息,而不是以某种方式禁用整个页面并仅显示表单,再一次,没有别的,而是转到主页。
      • 它显示什么消息并成功发送邮件。
      • 显示邮件已发送,它发送邮件但再次重定向到同一页面但只有表单可见。
      • 然后试试这个if ($mail_status) { echo "&lt;script type='text/javascript'&gt;window.location.href='https://saprs.000webhostapp.com/index.html'&lt;/script&gt;"; } else { echo "&lt;script type='text/javascript'&gt;window.location.href='contact_page.html'&lt;/script&gt;"; } 在此之后不要添加任何重定向。
      • 它说:404。这是一个错误。在此服务器上找不到请求的 URL。对不起,我对此真的很愚蠢,以前从未使用过php。
      猜你喜欢
      • 2015-10-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2021-04-03
      • 2015-08-07
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多