javascript - "Submit"按钮发送邮件但不重定向?

标签 javascript php forms redirect

这个脚本让我抓狂。这是一个简单的提交表格。我单击“提交”按钮,包含所有提交信息的电子邮件完美生成。

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

我尝试过 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 现在将它们全部发送到垃圾邮件。而且我无法让该死的东西重定向。

如果有人能在我把眼睛挖出来之前提供帮助,我将非常感激。 ^_^;;

<小时/>

编辑:我已经尝试了你们建议的一切。就好像脚本完全拒绝执行 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");
}

}    
}
?>

最佳答案

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

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

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

关于javascript - "Submit"按钮发送邮件但不重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551100/

相关文章:

javascript - 确认提交到特定页面

javascript - 使用 JavaScript 的 HTTP POST 的可下载数据链接

javascript - PushWoosh notificationCallback 未触发

javascript - ie9 与现代 html 和 css 语言的兼容性(跨浏览器 CSS)

java - 使用 Java 自动提交 HTML 表单以查找杂货店营业时间

javascript - 在输入类型 "loading..."元素中选择文件后显示文本 ="file"

javascript - @babel/polyfill (Babel 7 beta) 导致 SyntaxError

php - Codeigniter - 更改错误分隔符 - 将所有错误放在一个 div 标签中,而不是单独的

php - 重置 GridView 过滤器

php - pg_query() : Query failed: ERROR: operator does not exist: integer ~~ unknown\n