PDA

View Full Version : Website Contact Form - Help Needed...


That Guy
4th October 2009, 23:23
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):
<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):
<?
$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="customer@website.com";
}
}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@MYADDRESS.COM","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 :)

bandmich
5th October 2009, 02:49
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;
} */

bandmich
5th October 2009, 11:29
And to put away name and e-mail checking, try to cut out this one:

<?php
if(!$name){
$error_msg .= "Your name \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(!isset($email)){
if($email == ""){
$sender_email="customer@website.com";
}
}else{
$sender_email=$email;
}
}
?>

ELCaD
5th October 2009, 12:22
This was very quick, but try this;

<?
$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=""; //had to strip email because I can't post urls...
}
}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("//had to strip email because I can't post urls...","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>';

Kev Jaques
5th October 2009, 12:35
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!

That Guy
5th October 2009, 13:04
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.

That Guy
5th October 2009, 13:16
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:

<?php
if(!$name){
$error_msg .= "Your name \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(!isset($email)){
if($email == ""){
$sender_email="customer@website.com";
}
}else{
$sender_email=$email;
}
}
?>


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.

Easy Credit Mobiles
6th October 2009, 19:47
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");

That Guy
8th October 2009, 14:40
Just want to say a BIG THANK YOU to Graham at Effective Web Design (http://www.aerin.co.uk/)

He sorted this problem and got the project back on track!

Top Bloke :)

fisicx
8th October 2009, 15:09
I take it that the form worked as required.

That Guy
8th October 2009, 15:14
I take it that the form worked as required.

Better than expected Thanks Again :)

Made a custom style sheet for it too so looking very swish :D

bandmich
8th October 2009, 15:21
Congratulation! :)