Contact Form with Captcha Confirmation using PDO in PHP
Submitted by alpha_luna on Thursday, July 28, 2016 - 12:20.
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.
This is the form where you can enter your full name, email, message and the captcha confirmation to contact any certain websites.
The user enter their information and message to contact any websites.
This is the result after enter all information, message, and the captcha confirmation as you can see in the image below.
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.
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.- <form class="form-horizontal" method="POST" action="contact_query.php">
- <p class="full_name">
- <input type="text" name="full_name" id="full_name" placeholder="" autofocus="autofocus" required/>
- </p>
- <p class="email">
- <input type="email" name="email" id="email" placeholder="" required/>
- </p>
- <p class="message">
- </p>
- <div class="control-group" style="float:left; margin-left:-185px;">
- <div class="controls">
- <img src="generatecaptcha.php?rand=<?php echo rand(); ?>" name="captcha_img" id='image_captcha' >
- <script language='JavaScript' type='text/javascript'>
- function refreshing_Captcha()
- {
- var img = document.images['image_captcha'];
- img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
- }
- </script>
- </div>
- </div>
- <br />
- <br />
- <br />
- <div class="control-group" style="margin-left:-181px;">
- <div class="controls">
- </div>
- </div>
- <div class="control-group" style="margin-left:-181px;">
- <div class="controls">
- </div>
- </div>
- </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".- <?php
- include ('db.php');
- $full_name = $_POST['full_name'];
- $email = $_POST['email'];
- $message = $_POST['message'];
- $code_confirmation = $_POST['code_confirmation'];
- echo "<script>alert('The captcha code does not match!!'); window.location='index.php'</script>";
- echo "<script>javascript:self-history.back() </script>;";
- } else {
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $insert_query = "INSERT INTO tbl_contact (full_name, email, message, code_confirmation)
- VALUES (?, ?, ?, ?)";
- $insert = $conn->prepare($insert_query);
- echo "<script>alert('Successfully send your message!'); window.location='index.php'</script>";
- }
- }
- ?>
Output
This is the form where you can enter your full name, email, message and the captcha confirmation to contact any certain websites.
The user enter their information and message to contact any websites.
This is the result after enter all information, message, and the captcha confirmation as you can see in the image below.
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
- 820 views