Website Contact Form - Help Needed...

That Guy

Free Member
Dec 23, 2008
1,683
236
UK
I am working on a contact form on a website and it works ok. But for what I need it for its over the top. The form has 3 fields which are:

Name:

Email:

Comments:


The problem is the script checks to see if the email address is valid and all fields are filled in. Also when the form is submitted it shows a summary page of what's been sent.

All I want is for the form to submit (not matter what is filled in) and automatically redirect to the homepage when sent instead of show up a confirmation screen.

Can anyone edit the php file for me? Here what I have:

Form Code on Webpage (Shouldn't need editing):
Code:
<div id="contact">
<form action="contact_form.php" method="post" >
Name<br /><input name="name" type="text" class="contactform" /><br />
E-mail<br /><input name="email" type="text" class="contactform" /><br />
Questions, Comments and Quotation Request<br /><textarea name="comments" rows="10" cols="80" class="contactformbox"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form>
</div>

PHP File (Needs simplifying):
Code:
<?
$name     = $_POST['name'];
$address  = $_POST['address'];
$state    = $_POST['state'];
$city     = $_POST['city'];
$zip      = $_POST['zip'];
$country  = $_POST['country'];
$phone    = $_POST['phone'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$fax      = $_POST['fax'];
$error_msg = "";
$msg = "";

if(!$name){
    $error_msg .= "Your name \n";
}
if($name){
    $msg .= "Name: \t $name \n";
}

if($address){
    $msg .= "Address: \t $address \n";
}

if($phone){
    $msg .= "Phone: \t $phone \n";
}

if(!$email){
    $error_msg .= "Your email \n";
}
if($email){
    if(!eregi("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\._\-]+\.[a-zA-Z]{2,4}", $email)){
        echo "\n<br>That is not a valid email address.  Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.\n<br>";
        exit;
    }            
    $msg .= "Email: \t $email \n";
}
 
if(!trim($comments)){
    $error_msg .= "Your comments \n";
}
if($comments){
    $msg .= "Comments: \t $comments \n";
}
$sender_email="";

if(!isset($name)){
    if($name == ""){
        $sender_name="Web Customer";
    }
}else{
    $sender_name=$name;
}
if(!isset($email)){
    if($email == ""){
        $sender_email="[email protected]";
    }
}else{
    $sender_email=$email;
}
if($error_msg != ""){
    echo "You didn't fill in these required fields:<br>"
    .nl2br($error_msg) .'<br>Please <a href="javascript:history.back()">return</a> to the previous page and try again.';
    exit;
}
$mailheaders  = "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$mailheaders .= "From: $sender_name <$sender_email>\r\n";
$mailheaders .= "Reply-To: $sender_email <$sender_email>\r\n"; 
mail("[email protected]","Contact From Website",stripslashes($msg), $mailheaders);
echo "<html>\n<head>\n<title>Thanks For Your Submission</title>\n</head>\n<body>\n<h2>Thank you for your feedback $name</h2>\n";echo '<b>This is the information you submitted</b>'."<br>\n";
echo nl2br(stripslashes($msg));
echo '<br><br></body></html>';
?>

I hope someone can help. I will be adding Captcha to the form after this is sorted.

Thanks in Advance :)
 
Do you see difference ? :)


/*if($error_msg != ""){
echo "You didn't fill in these required fields:<br>"
.nl2br($error_msg) .'<br>Please <a href="javascript:history.back()">return</a> to the previous page and try again.';
exit;
} */
 
Last edited:
Upvote 0
And to put away name and e-mail checking, try to cut out this one:
Code:
[COLOR=#000000][COLOR=#0000BB]<?php 
[/COLOR][COLOR=#007700]if(![/COLOR][COLOR=#0000BB]$name[/COLOR][COLOR=#007700]){  
    [/COLOR][COLOR=#0000BB]$error_msg [/COLOR][COLOR=#007700].= [/COLOR][COLOR=#DD0000]"Your name \n"[/COLOR][COLOR=#007700];  
} 
---------------------------------------------- 
if([/COLOR][COLOR=#0000BB]$email[/COLOR][COLOR=#007700]){  
    if(![/COLOR][COLOR=#0000BB]eregi[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\._\-]+\.[a-zA-Z]{2,4}"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]$email[/COLOR][COLOR=#007700])){  
        echo [/COLOR][COLOR=#DD0000]"\n<br>That is not a valid email address.  Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.\n<br>"[/COLOR][COLOR=#007700];  
        exit;  
    }              
    [/COLOR][COLOR=#0000BB]$msg [/COLOR][COLOR=#007700].= [/COLOR][COLOR=#DD0000]"Email: \t $email \n"[/COLOR][COLOR=#007700];  
} 
------------------------------------------ 
if(!isset([/COLOR][COLOR=#0000BB]$email[/COLOR][COLOR=#007700])){  
    if([/COLOR][COLOR=#0000BB]$email [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#DD0000]""[/COLOR][COLOR=#007700]){  
        [/COLOR][COLOR=#0000BB]$sender_email[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#DD0000]"[email protected]"[/COLOR][COLOR=#007700];  
    }  
}else{  
    [/COLOR][COLOR=#0000BB]$sender_email[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000BB]$email[/COLOR][COLOR=#007700];  
} 
} 
[/COLOR][COLOR=#0000BB]?> [/COLOR][/COLOR]
 
Upvote 0
This was very quick, but try this;

Code:
<?
$name     = $_POST['name'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$error_msg = "";
$msg = "";

if($name){
    $msg .= "Name: \t $name \n";
}

if($email){
    $msg .= "Email: \t $email \n";
}
if($comments){
    $msg .= "Comments: \t $comments \n";
}
$sender_email="";

if(!isset($name)){
    if($name == ""){
        $sender_name="Web Customer";
    }
}else{
    $sender_name=$name;
}
if(!isset($email)){
    if($email == ""){
        $sender_email=""; [I]//had to strip email because I can't post urls...[/I]
    }
}else{
    $sender_email=$email;
}
$mailheaders  = "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$mailheaders .= "From: $sender_name <$sender_email>\r\n";
$mailheaders .= "Reply-To: $sender_email <$sender_email>\r\n"; 
mail("[I]//had to strip email because I can't post urls...[/I]","Contact From Website",stripslashes($msg), $mailheaders);
echo "<html>\n<head>\n<title>Thanks For Your Submission</title>\n</head>\n<body>\n<h2>Thank you for your feedback $name</h2>\n";echo '<b>This is the information you submitted</b>'."<br>\n";
echo nl2br(stripslashes($msg));
echo '<br><br></body></html>';
 
Last edited by a moderator:
Upvote 0
I think you are missing out on the point of the redirect!
If you point it to a confirmation page you can set up a goal for it in analytics but not for homepage.
It's also another page you have to be able to convince people they made the right choice.
You should also sanitize those inputs!
 
Upvote 0
I think you are missing out on the point of the redirect!
If you point it to a confirmation page you can set up a goal for it in analytics but not for homepage.
It's also another page you have to be able to convince people they made the right choice.
You should also sanitize those inputs!

Im not missing the point this is a custom project which I am working on. We are customising it to our needs.
 
Upvote 0
Do you see difference ? :)


/*if($error_msg != ""){
echo "You didn't fill in these required fields:<br>"
.nl2br($error_msg) .'<br>Please <a href="javascript:history.back()">return</a> to the previous page and try again.';
exit;
} */

And to put away name and e-mail checking, try to cut out this one:
Code:
[COLOR=#000000][COLOR=#0000bb]<?php 
[/COLOR][COLOR=#007700]if(![/COLOR][COLOR=#0000bb]$name[/COLOR][COLOR=#007700]){  
    [/COLOR][COLOR=#0000bb]$error_msg [/COLOR][COLOR=#007700].= [/COLOR][COLOR=#dd0000]"Your name \n"[/COLOR][COLOR=#007700];  
} 
---------------------------------------------- 
if([/COLOR][COLOR=#0000bb]$email[/COLOR][COLOR=#007700]){  
    if(![/COLOR][COLOR=#0000bb]eregi[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\._\-]+\.[a-zA-Z]{2,4}"[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$email[/COLOR][COLOR=#007700])){  
        echo [/COLOR][COLOR=#dd0000]"\n<br>That is not a valid email address.  Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.\n<br>"[/COLOR][COLOR=#007700];  
        exit;  
    }              
    [/COLOR][COLOR=#0000bb]$msg [/COLOR][COLOR=#007700].= [/COLOR][COLOR=#dd0000]"Email: \t $email \n"[/COLOR][COLOR=#007700];  
} 
------------------------------------------ 
if(!isset([/COLOR][COLOR=#0000bb]$email[/COLOR][COLOR=#007700])){  
    if([/COLOR][COLOR=#0000bb]$email [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#dd0000]""[/COLOR][COLOR=#007700]){  
        [/COLOR][COLOR=#0000bb]$sender_email[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#dd0000]"[email protected]"[/COLOR][COLOR=#007700];  
    }  
}else{  
    [/COLOR][COLOR=#0000bb]$sender_email[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]$email[/COLOR][COLOR=#007700];  
} 
} 
[/COLOR][COLOR=#0000bb]?> [/COLOR][/COLOR]

Thanks for your help. The code changes didn't really make any difference.

I got to to the point that when a user submitted the form it to went to a blank white confirmation screen but still didnt re-direct back to the home page.
 
Upvote 0
Try adding this to the end of the php code. Change where i have thanks.htm to whatever your index page is with the right extension...

// Redirect
header("Location: thanks.htm");
 
Upvote 0

Latest Articles