Jump to content

Help W/ Email Form Script


loligator

Recommended Posts

I used some generator on a site to make a form that is sent to an email address when submitted. I made an error page for when it doesn't work, and it shows up every time I try to test the form-email feature. I don't know php, and was hoping someone could help me figure out why the script won't work. Here is a link to the site, the form is on the home page:http://livetest.200u.com/jakem/Thanks in advance!

Link to comment
Share on other sites

Somewhere in the root folder for the site there is a file called contact.php, or there should be - we need to see that code to help.

Link to comment
Share on other sites

You have a PHP File in your folder "jakem" its called "contact.php" Copy and Paste it in a new post.From what I looked at on http://livetest.200u.com/jakem/home.htm It looks as if you forgot to add id's to each field so your PHP is prob. trying to collect data from fields that aren't there...Your Code:

<form action="contact.php" method="post" name="ContactForm" style="font-size:10pt"><table><tbody><tr><td>Name:<br><input size="30" name="name" type="text"></td><td>City:<br><input size="30" name="city" type="text"></td></tr><tr><td>Phone:<br><input size="30" name="phone" type="text"></td><td>Email:<br><input size="30" name="email" type="text"></td></tr><tr><td colspan="2">Comments:<br><textarea name="comments" rows="4" cols="62"></textarea><br><br><input value="Submit" type="submit"></td></tr></tbody></table></form>

<input type="submit" name="send" id="send" value="Send">Should be...

<table><tbody><tr><td><label for="Name"></label><br><input size="30" name="name" type="text" id="name"></td><td><label for="City"></label><br><input size="30" name="city" type="text" id="city"></td></tr><tr><td><label for="Phone"></label><br><input size="30" name="phone" type="text"id="phone"></td><td><label for="Email"></label><br><input size="30" name="email" type="text" id="email"></td></tr><tr><td colspan="2"><label for="Comments"></label><br><textarea name="comments" rows="4" cols="62"></textarea><br><br><input value="Submit" type="submit" id="submit"></td></tr></tbody></table></form>

Link to comment
Share on other sites

Here is my PHP code, although I think your right about the id's because for some reason I had it in my head that it used the names on the fields. dumb.<?php// Website Contact Form Generator // http://www.tele-pro.co.uk/scripts/contact_form/ // This script is free to use as long as you // retain the credit link // get posted data into local variables$EmailFrom = "Derfus Site";$EmailTo = "jakemerringer@gmail.com";$Subject = "Contact Info";$Name = Trim(stripslashes($_POST['Name'])); $City = Trim(stripslashes($_POST['City'])); $Tel = Trim(stripslashes($_POST['Tel'])); $Email = Trim(stripslashes($_POST['Email'])); $Comment = Trim(stripslashes($_POST['Comment'])); // validation$validationOK=true;if (Trim($Name)=="") $validationOK=false;if (Trim($City)=="") $validationOK=false;if (Trim($Tel)=="") $validationOK=false;if (!is_numeric($Tel)) $validationOK=false;if (Trim($Email)=="") $validationOK=false;if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit;}// prepare email body text$Body = "";$Body .= "Name: ";$Body .= $Name;$Body .= "\n";$Body .= "City: ";$Body .= $City;$Body .= "\n";$Body .= "Tel: ";$Body .= $Tel;$Body .= "\n";$Body .= "Email: ";$Body .= $Email;$Body .= "\n";$Body .= "Comment: ";$Body .= $Comment;$Body .= "\n";// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");// redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.htm\">";}else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";}?>I guess I missed the last post. I tried adding id's anyways and it still sent me to the error.htm

Link to comment
Share on other sites

For this:$Name = Trim(stripslashes($_POST['Name'])); $City = Trim(stripslashes($_POST['City'])); $Tel = Trim(stripslashes($_POST['Tel'])); $Email = Trim(stripslashes($_POST['Email'])); $Comment = Trim(stripslashes($_POST['Comment']));Your post names need to match your field names. $_POST['name'] and $_POST['Name'] aren't the same. If that doesn't work, instead of redirecting have it just print a message saying why it failed. Also, your validation is checking if the phone number is numeric, if they enter non-numeric characters into that field it won't validate.

<?php// Website Contact Form Generator // http://www.tele-pro.co.uk/scripts/contact_form/ // This script is free to use as long as you // retain the credit link // get posted data into local variables$EmailFrom = "Derfus Site";$EmailTo = "jakemerringer@gmail.com";$Subject = "Contact Info";$Name = Trim(stripslashes($_POST['Name'])); $City = Trim(stripslashes($_POST['City'])); $Tel = Trim(stripslashes($_POST['Tel'])); $Email = Trim(stripslashes($_POST['Email'])); $Comment = Trim(stripslashes($_POST['Comment'])); // validation$validationOK=true;if (Trim($Name)=="") $validationOK=false;if (Trim($City)=="") $validationOK=false;if (Trim($Tel)=="") $validationOK=false;if (!is_numeric($Tel)) $validationOK=false;if (Trim($Email)=="") $validationOK=false;if (!$validationOK) {  print 'failed validation';  //print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";  exit;}// prepare email body text$Body = "";$Body .= "Name: ";$Body .= $Name;$Body .= "\n";$Body .= "City: ";$Body .= $City;$Body .= "\n";$Body .= "Tel: ";$Body .= $Tel;$Body .= "\n";$Body .= "Email: ";$Body .= $Email;$Body .= "\n";$Body .= "Comment: ";$Body .= $Comment;$Body .= "\n";// send email$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");// redirect to success pageif ($success){  print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.htm\">";}else{  print 'sending email failed';  //print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";}?>

Link to comment
Share on other sites

I changed the cases of the names but still no luck. I still get sent to the error.htm.Here is my new code:<?php// Website Contact Form Generator // http://www.tele-pro.co.uk/scripts/contact_form/ // This script is free to use as long as you // retain the credit link // get posted data into local variables$EmailFrom = "Derfus Site";$EmailTo = "jakemerringer@gmail.com";$Subject = "Contact Info";$name = Trim(stripslashes($_POST['Name'])); $city = Trim(stripslashes($_POST['City'])); $tel = Trim(stripslashes($_POST['Tel'])); $email = Trim(stripslashes($_POST['Email'])); $comment = Trim(stripslashes($_POST['Comment'])); // validation$validationOK=true;if (Trim($name)=="") $validationOK=false;if (Trim($city)=="") $validationOK=false;if (Trim($tel)=="") $validationOK=false;if (Trim($email)=="") $validationOK=false;if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit;}// prepare email body text$Body = "";$Body .= "Name: ";$Body .= $name;$Body .= "\n";$Body .= "City: ";$Body .= $city;$Body .= "\n";$Body .= "Tel: ";$Body .= $tel;$Body .= "\n";$Body .= "Email: ";$Body .= $email;$Body .= "\n";$Body .= "Comment: ";$Body .= $comment;$Body .= "\n";// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");// redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.htm\">";}else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";}?>

Link to comment
Share on other sites

It doesn't help if all it does is redirect, get it to stop redirecting and print a message. I included that change in the code I posted above. That will at least tell you if validation failed or if sending the mail failed, right now you don't know which is the case. If you want more granularity, instead of doing this:if (Trim($name)=="") $validationOK=false;if (Trim($city)=="") $validationOK=false;if (Trim($tel)=="") $validationOK=false;if (Trim($email)=="") $validationOK=false;have it print a different message for each variable, then if it's failing validation you can at least figure out which one is failing.Also, it's not necessary to trim the variables again there, you're already trimming the values when you get them from post.

Link to comment
Share on other sites

Ok I'm back. Been busy the last few days. I copy and pasted your code and uploaded it to the server. Now it just says "failed validation".
Hi ! Try this code ! <?php// Website Contact Form Generator// http://www.tele-pro.co.uk/scripts/contact_form/// This script is free to use as long as you// retain the credit link// get posted data into local variables$EmailFrom = "Derfus Site";$EmailTo = "jakemerringer@gmail.com";$Subject = "Contact Info";$name = Trim(stripslashes($_POST['name']));$city = Trim(stripslashes($_POST['city']));$tel = Trim(stripslashes($_POST['tel']));$email = Trim(stripslashes($_POST['email']));$comment = Trim(stripslashes($_POST['comments']));// validation$validationOK=true;if (Trim($name)=="") $validationOK=false;if (Trim($city)=="") $validationOK=false;if (Trim($tel)=="") $validationOK=false;if (Trim($email)=="") $validationOK=false;if (!$validationOK) {print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";exit;}// prepare email body text$Body = "";$Body .= "Name: ";$Body .= $name;$Body .= "\n";$Body .= "City: ";$Body .= $city;$Body .= "\n";$Body .= "Tel: ";$Body .= $tel;$Body .= "\n";$Body .= "Email: ";$Body .= $email;$Body .= "\n";$Body .= "Comment: ";$Body .= $comment;$Body .= "\n";//$headers = "From: <$EmailFrom>";$headers .= "MIME-Version: 1.0\r\n";$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";// send email$success = @mail($EmailTo, $Subject, $Body, $headers);// redirect to success pageif ($success){print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.htm\">";}else{print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";}?>
Link to comment
Share on other sites

If it says it's failing validation then it's running this part:

$validationOK=true;if (Trim($Name)=="") $validationOK=false;if (Trim($City)=="") $validationOK=false;if (Trim($Tel)=="") $validationOK=false;if (!is_numeric($Tel)) $validationOK=false;if (Trim($Email)=="") $validationOK=false;if (!$validationOK) {  print 'failed validation';  //print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";  exit;}

It's still trying to validate a numeric phone number there, but that's fine. So at least you know why it's redirecting, because the variables aren't filled out. You can probably remove the error messages and do the redirects again, on your error.htm page you'll probably want to add some language saying that validation failed, and say what the requirements are for validation to pass. Also, down on the bottom here:

if ($success){print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyou.htm\">";}else{print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";}

Instead of also redirecting to error.htm there (it will say validation failed), have it redirect instead to mail_error.htm, and create a new file with that name with a message that says that there was a problem sending the email for some reason. Technically speaking, it would redirect there if PHP could not find the mail server or if the mail server did not accept the message for delivery.

Link to comment
Share on other sites

I do have a error.htm that it redirects to with this message "The data you entered may be incorrect. Please go back and recheck the information you entered. All entries must be filled." and I'm going to take out the integer requirement for the "Tel" field. However, I have always entered just integers and it still takes me to the error page. I did get to the success page once but it still didn't send the form info to my inbox.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...