How to redirect PHP after code is executed

Ok,hello everyone ! :slight_smile: my first post on this forum and i hope you can help me (i know you can).
I have a pretty basic Contact form that i put together from couple of contact form samples found on the internet.There is some basic JavaScript which checks if all fields are entered correctly and if they are,it sends the email with a comment to my specified email address.The only problem is that at the end of the PHP code that sends the email,it brings up a new (blank,white) page with a basic link back to my index.html file (the user needs to click a link on that blank page to go back to the original website).
That is what i DON’T WANT in my php. i want my php to automaticly REDIRECT the user,after he clicks on “send” button to one spcialy made html page if the sending was successful and to another if it wasn’t.
Is there a simple way to do it?
[php]<?php session_start();
if(isset($_POST[‘submit’])) {
$youremail = ‘[email protected]’;
$fromsubject = ‘www.submesubyou.co.cc’;
$name = $_POST[‘name’];
$last_name = $_POST[‘last_name’];
$email = $_POST[‘email’];
$subject = $_POST[‘subject’];
$message = $_POST[‘message’];
$to = $youremail;
$mailsubject = ‘Masage recived from’.$fromsubject.’ Contact Page’;
$body = $fromsubject.’

The person that contacted you is  '.$name.' '.$last_name.'
 E-mail: '.$email.'
 Subject: '.$subject.'

 Message: 
 '.$message.'

|---------END MESSAGE----------|'; 

echo “Thank you fo your feedback. I will contact you as soon as possible.
Go to Home Page”; /*want it changed so that it goes straight to the email_sent.html without need to click a link */
mail($to, $subject, $body);
} else {
echo “You must write a message. Please go to Contact Page”; /*want it changed so that it goes straight to the email_not_sent.html without need to click a link */
}
?> [/php]

Provided nothing is output above the line of code I give you, this should work (and if the code you posted is all that is being run on the page you are referring to, then it should).

[php]header(“Location: email_not_sent.html”);[/php]

Should i put that line after the echo statement or? Can you please copy-paste it into my code as an example where it should go? Because,I did a search online before i came here to post my problem and i tried exactly the same thing,but it didn’t work,probably because i didn’t put it in the right place in the code. :confused:
Sorry for beeing a pain in the abs lol

If you want to go straight from the PHP processing that stuff at the top to the redirect, then change the bottom of your code to this:

[php] } else {
header(‘Location: email_not_sent.html’);
}[/php]

If you want people to read a message first, you will need to use this:
[php] } else {
echo “You must write a message. You will be redirected back to the form shortly.”;
?>

<?php }[/php] (the above script assumes a 2 second wait time)

Thanks a lot man for your help :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service