“提交”按钮发送邮件但不重定向?

时间:2014-03-21 05:41:23

标签: javascript php forms redirect

这个剧本让我起了作用。这是一个简单的提交表单。我点击了"提交"按钮和包含所有提交信息的电子邮件生成完全正常。

但是我无法获得按钮,然后将我重定向到"谢谢你"页。

我尝试过PHP,我尝试过Javascript,我甚至尝试过老式的Meta Redirect。什么都行不通。

// 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();
?>

我已经尝试过把#34;标题"部分位于文档顶部。我已经尝试将其更改为:

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

我用这个脚本生成了很多电子邮件,gmail现在将它们全部发送到垃圾邮件。我无法改变这个该死的东西。

如果有人能在我睁开眼睛之前提供帮助,那将是非常有必要的。 ^ _ ^ ;;


编辑:我已经尝试过你所有建议的所有内容。好像脚本刚刚完全拒绝执行mail命令之后的任何内容。可能有这个原因吗?

编辑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");
}

}    
}
?>

4 个答案:

答案 0 :(得分:0)

使用location.href

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

可以在没有窗口前缀的情况下编写window.location对象。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

试试这个:

$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")行中也缺少分号,但我猜这是拼写错误。

答案 3 :(得分:0)

该行

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

需要

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

注意大写&#34; L&#34;,冒号后面的空格,以及结尾处的分号。

如果这不能解决您的问题,那么您在其他一些代码中遇到了问题。要找到它,您可以尝试查看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错误日志的标准位置。