1

This script is driving me up the wall. It's a simple submission form. I click the "submit" button and the email with all the submitted information is generated perfectly fine.

But I can't get the button to then redirect me to the "Thank You" page.

I've tried PHP, I've tried Javascript, I've even tried good old fashioned Meta Redirect. Nothing works.

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

I've tried putting the "header" part at the top of the document. I've tried changing it to:

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

I've generated so many emails with this script that gmail is now sending them all to spam. And I can't get the damn thing to redirect.

If anyone can help before I claw my eyes out, it would be much obliged. ^_^;;


EDIT: I've tried everything you've all suggested. It's as if the script just flat-out refuses to execute anything that comes after the mail command. Could there be a reason for this?

EDIT 2: Still nothing's working.

Here's the entire script (with Rolen Koh's modifications). Is there something hidden in here that is preventing the script from accessing anything that comes after the mail tag?

<?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 Answers4

0

use location.href

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

The window.location object can be written without the window prefix.

Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
0

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <!DOCTYPE ...> declaration, for example).

try this,this is what I am using

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

Try this:

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

Also semi-colon is missing in your header("location:http://amvleague.vitaminh.info/thankyou.html") line but i guess it is typo error.

Rolen Koh
  • 719
  • 2
  • 11
  • 21
0

The line

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

Needs to be

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

Note the capital "L", the space after the colon, and the semicolon at the end.

If this does not resolve your issue, then you have an issue in some other piece of code. To find it, you might try looking at the php error log. If you have access to the server, you can find this by using any of the following resources for your particular server.

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?

If you are on a shared host, they might have some non-standard location for this file, in which case, it might be easiest to contact them and ask where their standard location of the php error log is.

Community
  • 1
  • 1
Ben
  • 713
  • 4
  • 12