Page forwarding from contact.php page

Messages
1,570
Name
Brian
Edit My Images
Yes
Any scripting guru's out there that may be able to help with a script on my contact form.

I want the page to automatically go to the index page after a message has been sent, after the "thank you" bit.
Also, (I don't ask for much do I) cough cough:nuts:, if someone doesn't enter their email address they get a message telling them, is there a way to get the page to go back to the form?

Thank you in anticipation.

The code I use at the moment is:

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered, please use the back button on your browser to correct";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');

$your_email = "bt@durhamcitystudios.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

if(@mail($your_email,$email_subject,$email_content)) {
echo 'Thank you, we will be in touch soon!';
} else {
echo 'ERROR!';
}
}
?>
 
Ok, simple one first, in PHP you can't output any content before doing a header redirect unless you use javascript or a meta refresh both of which I wouldn't recommend, so its better to send the user to a success page, an example would be...

PHP:
header("location: success.php?type=contact");

PHP:
if($_GET['type'] == 'contact'){
echo 'thank you for filling out the contact form';
}

there are a few advantages of this method, one is you can track it easily in your analytics and also its a very good page for promoting other services, as people only see it if they are interested in what you are offering
 
Last edited:
validation - the complicated one ...

there are a whole host of different ways to approach to the problem of displaying error messages, a simple solution I'd recommend is to have client-side (javascript) validation on the form (http://docs.jquery.com/Plugins/Validation is my current fave), and also have matching server-side (php) validation on the processing script, the chances of someone hitting the php validation should be fairly remote (unless they are a bot) so you won't really need to worry about redirecting back to the form or showing relevant error messages, although there are ways of achieving this if you feel you really need it.

Another approach is to have the form posting to itself, this way you can check for errors at the top of the page, if there are any you can show the error messages, and re-populate the posted data back into the form all in one shot. If there aren't any errors you can show the success message instead of the form. It's not a particularly elegant solution, but it should be fine for a smallish website.

Also I'd be wary about using such a complex regex unless you know exactly how it works, there's nearly always an unusual email format that will fail the rule, at the end of the day you want to put as few obstacles in a customers way as possible, so just check for the basic email structure of
Code:
something@something.something
 
Back
Top