Contact Form
Posted: Tue May 17, 2011 8:30 am
Hi,
I copied a contact form from a php book which I have used for a couple of years and I find it great as it does what I want but now I am about stunned on 2 things.
1) - I want to create a form where my user doesn't need to put anything inside that particular field (so it is mandatory). I still want validation that if they put something in there then it can only be letters and not numbers.
2) I have never ever been able to understand how to validate numbers only into a field as well.
Any help please?
Here is the php coding I have so loved and trusted over the years.
I copied a contact form from a php book which I have used for a couple of years and I find it great as it does what I want but now I am about stunned on 2 things.
1) - I want to create a form where my user doesn't need to put anything inside that particular field (so it is mandatory). I still want validation that if they put something in there then it can only be letters and not numbers.
2) I have never ever been able to understand how to validate numbers only into a field as well.
Any help please?
Here is the php coding I have so loved and trusted over the years.
<?php /* Data Variables */ //Title if (!empty($_REQUEST['title'])) { $title = ($_REQUEST['title']); } else { $title = NULL; echo "Please enter your <u>Title </u>.<br>"; } //First Name if (!empty($_REQUEST['firstname'])) { $firstname = stripslashes(htmlspecialchars($_REQUEST['firstname'])); } else { $firstname = NULL; echo "Please enter your <u>first name</u>.<br>"; } //Last Name if (!empty($_REQUEST['lastname'])) { $lastname = stripslashes(htmlspecialchars($_REQUEST['lastname'])); } else { $lastname = NULL; echo "Please enter your <u>last name</u>.<br>"; } //Position if (!empty($_REQUEST['position'])) { $position = stripslashes(htmlspecialchars($_REQUEST['position'])); } else { $position = NULL; echo "Please enter your <u>position</u>.<br>"; } //Company if (!empty($_REQUEST['company'])) { $company = stripslashes($_REQUEST['company']); } else { $company = NULL; echo "Please enter your <u>company</u>.<br>"; } //tel if (!empty($_REQUEST['tel'])) { $tel = stripslashes($_REQUEST['tel']); } else { $tel = NULL; echo "Please enter your <u>telephone number</u>.<br>"; } // Address 1 if (!empty($_REQUEST['address1'])) { $address1 = stripslashes($_REQUEST['address1']); } else { $address1 = NULL; echo "Please enter the first line of your address<br>"; } // Address 2 if (!empty($_REQUEST['address2'])) { $address2 = stripslashes(htmlspecialchars($_REQUEST['address2'])); } else { $address2 = NULL; echo "Please enter the second line of your address or type in n/a<br>"; } // Address 3 if (!empty($_REQUEST['address3'])) { $address3 = stripslashes(htmlspecialchars($_REQUEST['address3'])); } else { $address3 = NULL; echo "Please enter the third line of your address or type in n/a <br>"; } // Town/City if (!empty($_REQUEST['town'])) { $town = stripslashes(htmlspecialchars($_REQUEST['town'])); } else { $town = NULL; echo "Please enter your <u> Town/City </u>.<br>"; } // Post Code if (!empty($_REQUEST['postcode'])) { $postcode = stripslashes(htmlspecialchars($_REQUEST['postcode'])); } else { $postcode = NULL; echo "Please enter your <u> post code </u>.<br>"; } //Email Address $email =($_REQUEST['email']); if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)); else { $email = NULL; echo "Please enter your <u>Email Address</u>.<br>"; } // How if (!empty($_REQUEST['how'])) { $how = stripslashes(htmlspecialchars($_REQUEST['how'])); } else { $how = NULL; echo "Please enter <u>How you found us </u>.<br>"; } // Other if (!empty($_REQUEST['other'])) { $other = stripslashes(htmlspecialchars($_REQUEST['other'])); } else { $other = NULL; echo "Please enter where your found us or type n/a </u>.<br>"; } if ( $title && $firstname && $lastname && $position && $company && $address1 && $address2 && $address3 && $postcode && $tel && $email && $town && $other && $how ) { $to = "info@test.com"; $subject = "$firstname filled in form from A TESTING SITE"; $body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <title></title> </head> <body bgcolor='#f9daa9' text='#000000'> <center><table width='530' bgcolor='#ffffcc'> <tr> <td width='530'> <table border='0' width='500' cellpadding='0'> <tr align='center'> <img src='http://www.*******.com/templatefiles/03cf2ceb-bdbc-4723-ae49-c5c8e22ad565.jpg' width='525' height='150' border='0' style='background-position:center;' /> </td> </tr> <tr></tr> <tr> <td> <table width='92%' align='center'> <tr> <td colspan='4'> </td> </tr> <tr> <td colspan='4'><hr align='center' size='1' noshade='noshade' color='#000000'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Title:</u></div></td> <td width='1%'> </td> <td width='58%'> $title</td> <td width='15%'> </td> </tr> <tr> <td width='26%'><div align='right'><u>First Name:</u></div></td> <td width='1%'> </td> <td width='58%'> $firstname </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u>Last Name:</u></div></td> <td width='1%'> </td> <td width='58%'> $lastname </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u>Telephone Number:</u></div></td> <td width='1%'> </td> <td width='58%'> $tel </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Email Address:</u></div></td> <td width='1%'> </td> <td width='58%'> $email</td> <td width='15%'> </td> </tr> <tr> <td colspan='4'><hr align='center' size='1' noshade='noshade' color='#000000'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Company:</u></div></td> <td width='1%'> </td> <td width='58%'> $company</td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Position:</u></div></td> <td width='1%'> </td> <td width='58%'> $position</td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Address 1:</u></div></td> <td width='1%'> </td> <td width='58%'> $address1 </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Address 2:</u></div></td> <td width='1%'> </td> <td width='58%'> $address2 </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Address 3:</u></div></td> <td width='1%'> </td> <td width='58%'> $address3 </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> Town:</u></div></td> <td width='1%'> </td> <td width='58%'> $town </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> postcode:</u></div></td> <td width='1%'> </td> <td width='58%'> $postcode </td> <td width='15%'> </td> </tr> <tr> <td colspan='4'><hr align='center' size='1' noshade='noshade' color='#000000'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u> How Did you find us:</u></div></td> <td width='1%'> </td> <td width='58%'> $how</td> <td width='15%'> </td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td colspan='4'></td> </tr> <tr> <td width='26%'><div align='right'><u>Other:</u></div></td> <td width='1%'> </td> <td width='58%'> $other</td> <td width='15%'> </td> </tr> <tr> <td colspan='4'><hr align='center' size='1' noshade='noshade' color='000000'></td> </tr> <tr> <td colspan='4'> </td> </tr> <tr> <td colspan='4'> </td> <tr> <td> </td> <td> </td> <td colspan='2'> </td> </tr> <tr> <td colspan='4'></td> </tr> </table> </td> </tr> <tr> <td bgcolor='#ffffcc' style='color:#000000;' height='20' align='center'><small> Copyright 2011 © tester. All Rights Reserved.</small></center></td> </tr> </table> </td> </tr> </table> </body></center> </html>"; $headers = 'From: finance@testing.com' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to,$subject,$body,$headers); echo "<p>Thank you for your enquiry $firstname!<br>We will be in touch soon.</p> <p> </p> <p> You will be re-directed to our home page in 5 seconds</p>\n"; echo "<meta http-equiv=\"Refresh\" content=\"5;url = http://www.test@testing.com/\">"; } else { echo "<br>Please go back and fill in the missing field(s)."; } ?>