Simple PHP Contact Form + jscript validation + anti-bot


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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.  
  123. // where the email will look like it is sent from
  124.  
  125. $subject = "Email from your website";
  126.  
  127. $body = "Email from your website" . "
  128.  
  129. ";
  130. $body .= "Name: " . $FirstName . $LastName . "
  131. ";
  132. $body .= "Email: " . $email . "
  133. ";
  134. $body .= "Newsletter: " . $subscribe . "
  135. ";
  136. $body .= "Comments: " . $comments . "
  137. ";
  138.  
  139. $headers = "From: $from" . "
  140. ";
  141. $headers .= "Reply-To: $from" . "
  142. ";
  143. $headers .= "Return-Path: $from" . "
  144. ";
  145.  
  146. // mail(to,subject,body,headers);
  147.  
  148. $isMailed = mail($to, $subject, $body, $headers);
  149.  
  150. if($isMailed){
  151. echo "Thank you for your inquery, " . $FirstName . " " . $LastName . " We will get back to you shortly.";
  152. }else{
  153. echo "There seemed to be a problem";
  154. }
  155.  
  156. ?>

URL: http://www.thatgrafix.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.