【问题标题】:"Submit" button sends mail but doesn't redirect?“提交”按钮发送邮件但不重定向?
【发布时间】:2014-04-28 08:50:18
【问题描述】:

这个脚本把我逼疯了。这是一个简单的提交表单。我点击“提交”按钮,包含所有提交信息的电子邮件都生成得非常好。

但我无法获得将我重定向到“谢谢”页面的按钮。

我尝试过 PHP,我尝试过 Javascript,我什至尝试过老式的元重定向。没有任何效果。

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  

header("location:http://amvleague.vitaminh.info/thankyou.html")
}

die();
?>

我尝试将“标题”部分放在文档的顶部。我尝试将其更改为:

echo '<script>document.location="page2.html"; </script>';

我使用此脚本生成了如此多的电子邮件,以至于 gmail 现在将它们全部发送到垃圾邮件中。而且我无法让该死的东西重定向。

如果有人能在我睁大眼睛之前提供帮助,我将不胜感激。 ^_^;;


编辑:我已经尝试了你所建议的一切。就好像脚本完全拒绝执行邮件命令之后的任何内容。这有什么原因吗?

编辑 2:仍然没有任何效果。

这是整个脚本(Rolen Koh 的修改)。这里是否隐藏了一些东西,阻止脚本访问邮件标签之后的任何内容?

<?php
if(isset($_POST['email'])) {
    $email_to = "pathos@vitaminh.info";
    $email_subject = "BelleCON 2014 - AMV League Submission";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form     you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['handle']) ||
    !isset($_POST['amv_title']) ||
    !isset($_POST['amv_song']) ||
    !isset($_POST['amv_artist']) ||
    !isset($_POST['amv_anime']) ||
    !isset($_POST['amv_link']) ||
    !isset($_POST['amv_category']) ||
    !isset($_POST['email'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

function IsChecked($chkname,$value)
{
    if(!empty($_POST[$chkname]))
    {
        foreach($_POST[$chkname] as $chkval)
        {
            if($chkval == $value)
            {
                return true;
            }
        }
    }
    return false;
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$handle = $_POST['handle']; // not required
$amv_title = $_POST['amv_title']; // required
$amv_song = $_POST['amv_song']; // required
$amv_artist = $_POST['amv_artist']; // required
$amv_anime = $_POST['amv_anime']; // required
$amv_link = $_POST['amv_link']; // required
$amv_category = $_POST['amv_category']; // required
$email_from = $_POST['email']; // required


$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "/^[A-Za-z .-]+$/";
  if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
    if(strlen($error_message) > 0) {
died($error_message);
  }
    $email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

    $email_message .= "Name: ".clean_string($first_name).clean_string($last_name)."\n";
$email_message .= "Handle: ".clean_string($handle)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Title of AMV: ".clean_string($amv_title)."\n";    
$email_message .= "Category: ".clean_string($amv_category)."\n";
$email_message .= "Song: ".clean_string($amv_song)." by     ".clean_string($amv_artist)."\n";
$email_message .= "Anime Used: ".clean_string($amv_anime)."\n\n";
$email_message .= clean_string($amv_link)."\n";        

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail = mail($email_to, $email_subject, $email_message, $headers);  
if($mail)
{
header("location:http://amvleague.vitaminh.info/thankyou.html");
}

}    
}
?>

【问题讨论】:

  • 请在此处检查您提供的路径是否正确。
  • 删除 header() 之前的空格。并尝试调用 exit();在 header(); 之后
  • 我已经提出了一个答案。请在下方查看并尝试一下。

标签: javascript php forms redirect


【解决方案1】:

使用location.href

echo '<script>window.location.href="page2.html"; </script>';

window.location对象可以不用window前缀写。

【讨论】:

    【解决方案2】:

    您可以使用 header() 函数发送新的 HTTP 标头,但这必须在任何 HTML 或文本之前发送到浏览器(例如,在 &lt;!DOCTYPE ...&gt; 声明之前)。

    试试这个,这是我正在使用的

     function redirect($url){
        if (headers_sent()){
          die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
        }else{
          header('Location: ' . $url);
          die();
        }    
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      $mail = mail($email_to, $email_subject, $email_message, $headers);  
      if($mail)
      {
          header("location:http://amvleague.vitaminh.info/thankyou.html");
      }
      

      您的header("location:http://amvleague.vitaminh.info/thankyou.html") 行中也缺少分号,但我猜这是拼写错误。

      【讨论】:

        【解决方案4】:

        线

        header("location:http://amvleague.vitaminh.info/thankyou.html")
        

        需要

        header("Location: http://amvleague.vitaminh.info/thankyou.html");
        

        注意大写“L”、冒号后面的空格和末尾的分号。

        如果这不能解决您的问题,那么您在其他一些代码中存在问题。要找到它,您可以尝试查看 php 错误日志。如果您有权访问该服务器,则可以使用以下任何适用于您的特定服务器的资源来找到它。

        http://www.cyberciti.biz/faq/error_log-defines-file-where-script-errors-logged/ Where does PHP store the error log? (php5, apache, fastcgi, cpanel) Where can I find error log files?

        如果您在共享主机上,他们可能对此文件有一些非标准位置,在这种情况下,最容易联系他们并询问他们的 php 错误日志的标准位置在哪里。

        【讨论】:

          猜你喜欢
          • 2013-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-09
          • 1970-01-01
          • 2021-10-09
          • 1970-01-01
          • 2013-04-04
          相关资源
          最近更新 更多