Contact Us form redirect to home page after submit

I have developed a contact us page and also PHP code that works perfect. Know the only problem is that after submitting the form it shows only “Message sent” on a new page. I want that after submitting the form page will redirect back to home page. It is only one page “Landing Page”. Here is the PHP code.`

<?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";
    	$errors=1;
    }
    if($errors==1) echo $error;
    else{
    	$values = array ('name','email','message','phone');
    	$required = array('name','email','message','phone');
    	
    	$your_email = "todd@toddandersonhomes.com";
    	$email_subject = "Free Credit Report: ".$_POST['subject'];
    	$email_content = "Following is the free credit report detail from client:\
";
    	
    	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]."\
";
    	  }
    	}
    	
    	if(@mail($your_email,$email_subject,$email_content)) {
    		echo 'Message sent!';
    	} else {
    		echo 'ERROR!';
    	}
    }
    ?>
    

HTML Code:

 <form action="contact.php" method="post">

                        	<div class="formele">
    				<ul>
    					<li>
    					<label>Name:</label>
    					<input type="text" id="name" name="name" class="text2" />
    					</li>
    					<li>
    					<label>Telephone:</label>
    					<input id="name" type="text" name="phone" class="text2" />
    					</li>
    					<li>
    					<label>E-mail:</label>
    					<input id="name" type="text" name="email" class="text2" />
    					</li>
                        <li>
    					<label>Address:</label>
    					<input id="name" type="text" name="message" class="text2" />
    					</li>
    					
    					<li class="button">
    					<input type="image" type="text" src="images/submit.png" />					
    					</li>
    				</ul>			
    			</div><!--Contact Form Ends Here -->
                </form>


in the form tag: action=“/” or action=“” not sure

Right after the email is sent, instead of this message
echo ‘Message sent!’;

use redirect like this:
header(“Location: your home page”);

your home page is the name of your Landing Page. Maybe not needed if it is index.php or index.html

Note that if your home page is at your parent directory make sure to use …/ before the name.

Or, you could just let your homepage handle it.
Stick the code to handle the submission in your homepage (or an include thereto) and just have the form point at the homepage.

(I try to avoid header() calls whenever possible.)

and Andres: it would be “/”. “” translates to “the current page” in form-speak.

Can you tell us why header() should be avoided? So what is the best way to perform redirect?

I avoid header calls because i tend to use a header include file. Which means my output has already been started, so i CANT send a header call.

In this particular scenario, consider what you’re telling him to do.
Person fills form out. Hits submit.
Form Processor Page loads for a brief instant (page 2 load)
Processor page sends Header. Browser loads new page (Page 3 load)
Index page loads.

In terms of your database, assuming that each page uses a database connection, you’ve connected 3 times.

If, however, you point the form directly at the index page, you dont need a third connection - you’ve already opened one for the index page, so you’ll just use that connection to process the form during the course of loading the index page.

Plus you dont have the browser making the ‘reload click’ noise an extra time. Which makes it better. :stuck_out_tongue:

Good points. Very often people come back and complain to me that the header() gave them error, such as header already sent message. However, it is common that we use the same script to process the form. This is so that validation can be performed and resubmitted if needed. Only if it passed validation that we perhaps want to call a different script. We could provide a link for the user to click on, or do this automatically using redirect such as header() that I suggested.

There’s no reason that you cant do validation at the header - if it fails, you can include_once('form.php); die(); (or, if you really felt the need, you could push a header and force a reload again).

As long as you use include_once instead of include, there’s no real need for it.

Good to know. Thanks!