Posted By

jadedbat on 02/23/09


Tagged

form php html


Versions (?)

Who likes this?

17 people have marked this snippet as a favorite

umang_nine
kurokikaze
garretjames
pezland
cjwilburn
m4tik
vali29
Pixxell
nb109
BrianCoyDesign
mikoli
mfigge
lfcortes
clac
ringo380
Mench59
ekarakas


Simple PHP Contact Form + jscript validation + anti-bot


 / Published in: PHP
 

URL: http://www.thatgrafix.com

A simple to use framework for a contact form. This is missing some serious validation and such but is a good startin' point for any simple contact form.

The input field named 'botty' is an input field we hide with css.. if data is sent to the handling file from that field then we know a non-human filled this form out.. and it kills the script.

** just added some simple jscript to handle some mild validation.

  1. CSS
  2. <style type="text/css"> #botty {display:none;} <style>
  3.  
  4. Javascript
  5. <script type="text/javascript">
  6.  
  7. function checkform(form){
  8.  
  9. // check first name
  10. if (form.FirstName.value == "") {
  11. alert( "Please enter your First Name." );
  12. form.FirstName.focus();
  13. return false ;
  14. }
  15.  
  16. // check last name
  17. if (form.LastName.value == "") {
  18. alert( "Please enter your Last Name." );
  19. form.LastName.focus();
  20. return false ;
  21. }
  22.  
  23. // check email field
  24. if (form.email.value == "") {
  25. alert( "Please enter your Email Address." );
  26. form.email.focus();
  27. return false ;
  28. }
  29.  
  30. // check for valid email addy
  31. var apos=form.email.value.indexOf("@");
  32. var dotpos=form.email.value.lastIndexOf(".");
  33.  
  34. if (apos<1||dotpos-apos<2){
  35. alert("Not A Valid Email Address!");
  36. return false;
  37. }
  38.  
  39. // ** END **
  40. return true ;
  41.  
  42. }
  43. </script>
  44.  
  45.  
  46.  
  47. HTML
  48. <form action="contact_handle.php" method="post" name="formy" id="formy">
  49. <p>* Required</p>
  50. <table cellspacing="0" cellpadding="2" border="0" class="webform">
  51. <tbody>
  52. <tr>
  53. <td><label for="Title">Title</label>
  54. <br />
  55. <select name="Title" id="Title" class="cat_dropdown_smaller">
  56. <option value="126631">DR</option>
  57. <option value="126629">MISS</option>
  58. <option value="126627" selected="selected">MR</option>
  59. <option value="126628">MRS</option>
  60. <option value="126630">MS</option>
  61. </select></td>
  62. </tr>
  63. <tr>
  64. <td><label for="FirstName">First Name</label>
  65. <br />
  66. <input name="FirstName" type="text" class="cat_textbox" id="FirstName" size="35" maxlength="255" />
  67. *</td>
  68. </tr>
  69. <tr>
  70. <td><label for="LastName">Last Name</label>
  71. <br />
  72. <input name="LastName" type="text" class="cat_textbox" id="LastName" size="35" maxlength="255" />
  73. *</td>
  74. </tr>
  75. <tr>
  76. <td><label for="email">Email Address</label>
  77. <br />
  78. <input name="email" type="text" class="cat_textbox" id="email" size="35" maxlength="255" />
  79. *</td>
  80. </tr>
  81. <tr>
  82. <td><label for="comments">Comments/Enquiries</label>
  83. <br />
  84. <textarea name="comments" cols="45" rows="8" class="cat_listbox" id="comments" onkeydown="if(this.value.length&gt;=1024)this.value=this.value.substring(0,1023);"></textarea></td>
  85. </tr>
  86. <tr>
  87. <td><input name="subscribe" type="checkbox" id="subscribe" value="yes" />
  88. Subscribe to: Email List Signup</td>
  89. </tr>
  90. <tr>
  91. <td><input type="submit" class="cat_button" value="Submit" id="catwebformbutton" />
  92. <span id="formHide"><input type="text" id="botty" name="botty" /></span>
  93. </td>
  94. </tr>
  95. </tbody>
  96. </table>
  97.  
  98. </form>
  99.  
  100.  
  101.  
  102. PHP
  103.  
  104. <?php
  105.  
  106. //form data
  107. $FirstName = strip_tags($_POST['FirstName']);
  108. $LastName = strip_tags($_POST['LastName']);
  109. $email = strip_tags($_POST['email']);
  110. $subscribe = strip_tags($_POST['subscribe']);
  111. $comments = strip_tags($_POST['comments']);
  112.  
  113. // prove if a bot has entered data
  114. $botty = strip_tags($_POST['botty']);
  115.  
  116. if($botty != NULL){
  117. echo "barghh! darn bot!";
  118. }
  119.  
  120.  
  121. // the email address where the script will email the form results to
  122. $to = "[email protected]";
  123.  
  124. // where the email will look like it is sent from
  125. $from = "[email protected]";
  126.  
  127. $subject = "Email from your website";
  128.  
  129. $body = "Email from your website" . "
  130.  
  131. ";
  132. $body .= "Name: " . $FirstName . $LastName . "
  133. ";
  134. $body .= "Email: " . $email . "
  135. ";
  136. $body .= "Newsletter: " . $subscribe . "
  137. ";
  138. $body .= "Comments: " . $comments . "
  139. ";
  140.  
  141. $headers = "From: $from" . "
  142. ";
  143. $headers .= "Reply-To: $from" . "
  144. ";
  145. $headers .= "Return-Path: $from" . "
  146. ";
  147.  
  148. // mail(to,subject,body,headers);
  149.  
  150. $isMailed = mail($to, $subject, $body, $headers);
  151.  
  152. if($isMailed){
  153. echo "Thank you for your inquery, " . $FirstName . " " . $LastName . " We will get back to you shortly.";
  154. }else{
  155. echo "There seemed to be a problem";
  156. }
  157.  
  158. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: markepp on April 7, 2010

Hi This is my first go at setting up a contact form, I have used the above but can't seem to get it working.

I suspect it is the link between the form and the php, when I try to test the form my browser is asking me if I wish to open the .php file!?!

I suspect it is within the form actions"" parameter but have tried a few things. Can someone talk me through set-up in basic terms,thanks Mark

Posted By: markepp on April 7, 2010

Hi This is my first go at setting up a contact form, I have used the above but can't seem to get it working.

I suspect it is the link between the form and the php, when I try to test the form my browser is asking me if I wish to open the .php file!?!

I suspect it is within the form actions"" parameter but have tried a few things. Can someone talk me through set-up in basic terms,thanks Mark

Posted By: markepp on April 8, 2010

Hi This is my first go at setting up a contact form, I have used the above but can't seem to get it working.

I suspect it is the link between the form and the php, when I try to test the form my browser is asking me if I wish to open the .php file!?!

I suspect it is within the form actions"" parameter but have tried a few things. Can someone talk me through set-up in basic terms,thanks Mark

Posted By: Suze on October 20, 2010

Mark, I see it was some time ago that you posted your question, and you presumably have found the answer by now. But I thought I would answer it anyway, since I created an account specifically to answer it, before I noticed the date, and it may help someone else!

My guess is that you are testing the script by attempting to open it in a web browser on your own computer. PHP doesn't work like that, it needs to be on a server, generally the browser can't understand php. When the script is run it creates an HTML web-page which is sent to your web-browser which it can display. You can't leave out the server because then the web-page is never generated.

You need to login to post a comment.