Contact Us form redirect to home page after submit

kaleem_ullah 0 Tallied Votes 3K Views Share

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:\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 '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>

Here is the link of the page: http://www.designsblessing.com/Landingpages/

<?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:\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 'Message sent!'; 
    	} else {
    		echo 'ERROR!';
    	}
    }
    ?>
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

You can use header to redirect.

Szabi Zsoldos 26 Learner and helper guy

header('Location: yourpage.extension') or you could use the window.location from Javascript.

cjohnweb 14 User Title? What's that?

If you are interested, I use sessions to hold user messages. I've create just a small handful of functions, and some css so it can be placed anywhere on any website and just work :)

Here we go:

function set_user_message($msg, $class = "warning"){
$_SESSION['user']['msg'][$class][] = $msg;
}

function read_user_message(){
foreach ($_SESSION['user']['msg'] as $k => $v){
foreach ($v as $kk => $vv){
echo "<div class=\"$k\">$vv</div>\n";
}
}
unset($_SESSION['user']['msg']);
}

function check_user_message(){
if(count($_SESSION['user']['msg']) > 0){
return(true);
}else{
return(false);
}
}

/*
Redirect user to new location, with status codes!
  MUST BE USED IN PHP BEFORE ANY TEXT IS CALLED TO 
  THE SCREEN, SENDS HEADER INFORMATION
302 = Temporary redirect (default)
301 = Permanent redirect
*/
function redirect($location,$status=302){
$str = explode("?",$location);
parse_str($str[1], $output);
foreach ($output as $k => $v){$uri .= "$k=".urlencode($v)."&";}
$uri = substr($uri,0,-1);
$location = $str[0];
if(!empty($uri)){$location .= "?".$uri;} 
@header("Location: $location", true, $status);
//header("Refresh: 0;url=$location");
}


function check_error_user_message(){
if(count($_SESSION['user']['msg']['error']) > 0){
return(true);
}else{
return(false);
}
}

/* Used on one site to make it align properly - display the user messages */
function user_messages(){
if(check_user_message() == true){
echo "<div style=\"float:left; width:100%; margin:10px; padding:10px; border:0px solid #000000;\">";
read_user_message();
echo "</div>";
echo "<br class=\"clear\" />";
}
}



.info, .success, .warning, .error, .validation, .search, .tips {
width:400px;
border: 1px solid;
margin: 0px auto;
margin-top:10px;
margin-bottom:10px;
padding:10px;
padding-left:50px;
background-repeat: no-repeat;
background-position: 10px center;
list-style:none;
}

.info ul, .success ul, .warning ul, .error ul, .validation ul, .search ul, .tips ul {
list-style:none;
}

.info a, .success a, .warning a, .error a, .validation a, .search a, .tips a {
text-decoration:none;
}

.tips {
border: 0px solid;
margin: 10px 0px;
padding:15px 10px 15px 50px;
background-repeat: no-repeat;
background-position: 10px center;
/*color: #00529B;
background-color: #BDE5F8;*/
font-size:16px;
color: #000000;
/*background-color: #FFD685;*/
background-image: url(images/tips.png);
}

.tips a,.tips a:visited,.tips a:active,.tips a:link,.tips a:hover{
font-weight:bold;
font-style:italic;
text-decoration:underline !important;
}

.search {
color: #00529B;
background-color: #BDE5F8;
background-image: url(images/search.png);
}

.info {
color: #00529B;
background-color: #BDE5F8;
background-image: (images/info.png);
}

.success {
color: #4F8A10;
background-color: #DFF2BF;
background-image:url(images/success.png);
}

.warning {
color: #9F6000;
background-color: #FEEFB3;
background-image: url(images/warning.png);
}

.error {
color: #D8000C;
background-color: #FFBABA;
background-image: url(images/error.png);
}

.validation {
color: #D63301;
background-color: #FFCCBA;
background-image: url(images/validation.png);
}

Save the css file in the root directiory, I name it user-messages.css, and link to it in your index, template or every page that will use the script.

Include the functions in your PHP where each page that displays messages can access it, and also so the pages that process your code can access it.

Here is example of how to use it:

<?php

// processing a login (short hand to show usage)

if(empty($_POST['email'])){set_user_message("An email is required to log in","error");}
if(empty($_POST['pass'])){set_user_message("A password is required to log in","error");}

// other error checking here

if(check_error_user_message() == true){
redirect("index.php?page=login"); // send them back to login page
}else{
//login the user here

set_user_message("Logged in successfully","success");

redirect("index.php?page=account"); //redirect them to their account page or w/e
}


-----------------

index.php

<DOCTYPE html>
<html>
<head>
</head>
<body>

<h1>Login</h1>

<?php user_messages(); ?>

<form>
<input name="email" />
...
</form>

</body>
</html>
?>

It's really easy to use, just set user messages, and read user messages. If you noticed, I have the check_error_user_message(); function. It tells me if an error has been set. Basically I can use the error mesage as an indicator that we should not even attempt to login the user because, say, they didn't enter a password. You can use it how ever you see fit though.

I started this whole thing because the redirect function has to be called before any text is placed on the screen. So I was trying to send messages in the url as $_GET info, and that was a mess. This is much much cleaner, the functions save everything in sessions so when you set a message you don't have have to worry about it any more, everything just works :)

Don't forget to start your scripts with session_start(); or sessions will not work.

Questions, comments, feedback and improvements are always welcome. Please let me know some things if you try it - ease of installing to your site, simplicity in understainding, remembering the, and of course any bugs.

Thanks

cjohnweb 14 User Title? What's that?

You can download the icons for this script here, http://iluvjohn.com/user-message-icons

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.