Contact Form with Captcha Confirmation using PDO in PHP

We are going to create Contact Form with Captcha Confirmation using PDO in PHP. These simple tutorial, you can download for free. I will teach you in step by step on how to create a contact form with captcha confirmation using PDO in PHP. Also, you can learn how to generate a random code and you can use this to protect your page from random spammers. You can use this also as a type of a simple test that the response is generated by a human being and it's a very common on other websites before submitting the information in the form field.

You will learn:

  • The user learns to generate a random code.
  • The user learns the basics of CSS styling.
  • The user learns to create contact form using HTML.
  • The user learns to create stylish contact form using CSS.

Creating Markup and Captcha Random Code

This simple source code, we are going to create our contact form where the user can type their message, full name, and email. Also, we construct the captcha random code for the confirmation before they submit their message.
  1. <form class="form-horizontal" method="POST" action="contact_query.php">
  2.  
  3.         <p class="full_name">
  4.                 <input type="text" name="full_name" id="full_name" placeholder="" autofocus="autofocus" required/>
  5.                 <label for="full_name">Your Name</label>
  6.         </p>
  7.                
  8.         <p class="email">
  9.                 <input type="email" name="email" id="email" placeholder="" required/>
  10.                 <label for="email">Your Email Address</label>
  11.         </p>   
  12.        
  13.         <p class="message">
  14.                 <textarea name="message" placeholder="" required></textarea>
  15.                 <label for="message">Your Message</label>
  16.         </p>
  17.        
  18. <div class="control-group" style="float:left; margin-left:-185px;">
  19.         <div class="controls">
  20.        
  21.         <img src="generatecaptcha.php?rand=<?php echo rand(); ?>" name="captcha_img" id='image_captcha' >
  22.         <a href='javascript: refreshing_Captcha();'><i class="icon-refresh icon-large"></i></a>
  23.         <script language='JavaScript' type='text/javascript'>
  24.                 function refreshing_Captcha()
  25.                 {
  26.                         var img = document.images['image_captcha'];
  27.                         img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
  28.                 }
  29.         </script>
  30.         </div>
  31. </div>
  32.  
  33. <br />
  34. <br />
  35. <br />
  36.  
  37. <div class="control-group" style="margin-left:-181px;">
  38.         <div class="controls">
  39.                 <input id="code" name="code_confirmation" type="text" placeholder="Enter the Code Above" required></td>
  40.         </div>
  41. </div>
  42. <div class="control-group" style="margin-left:-181px;">
  43.         <div class="controls">
  44.                 <button type="submit" name="send_message" class="btn btn-primary"><i class="icon-ok icon-large"></i> Submit</button>
  45.         </div>
  46. </div>
  47.  
  48. </form>

PHP Source Code in PDO

This is our contact query where they can save all the data from the users. Save it as "contact_query.php".
  1. <?php
  2. include ('db.php');
  3.  
  4. if(isset($_POST['send_message'])) {
  5.  
  6. $full_name = $_POST['full_name'];
  7. $email = $_POST['email'];
  8. $message = $_POST['message'];
  9. $code_confirmation = $_POST['code_confirmation'];
  10.  
  11. if(strcmp($_SESSION['code_confirmation'], $_POST['code_confirmation']) != 0) {
  12.         echo "<script>alert('The captcha code does not match!!'); window.location='index.php'</script>";
  13.         echo "<script>javascript:self-history.back() </script>;";
  14. } else {
  15.  
  16. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. $insert_query = "INSERT INTO tbl_contact (full_name, email, message, code_confirmation)
  18. VALUES (?, ?, ?, ?)";
  19.  
  20. $insert = $conn->prepare($insert_query);
  21. $insert->execute(array($full_name, $email, $message, $code_confirmation));
  22.  
  23. echo "<script>alert('Successfully send your message!'); window.location='index.php'</script>";
  24. }
  25. }
  26. ?>

Output


This is the form where you can enter your full name, email, message and the captcha confirmation to contact any certain websites. Result
The user enter their information and message to contact any websites. Result
This is the result after enter all information, message, and the captcha confirmation as you can see in the image below. Result
And, that's it. This is the steps on how to create simple Contact Form with Captcha Confirmation using PDO in PHP. Kindly click the "Download Code" button below for full source code. Thank you very much. Hope that this tutorial will help you a lot. If you are interested in programming, we have an example of programs that may help you even just in small ways. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much. Enjoy coding.

Add new comment