The contact form is one of the common features of most websites. Now, simple contact forms work just fine, but if you want to make them nicer, use AJAX when you create a simple PHP contact form with captcha validation to submit the form data.
One of the best benefits you get from AJAX is, there is no page load. And it is easy-to-integrate to any PHP and/or HTML website.
This is all ok, but why you should create with a captcha PHP validation?
First, there are so many competitors on the market right now who want to spam you continuously. And to avoid that spamming, it is necessary to include captcha validation in PHP website.
So in this PHP tutorial, we’ll make one sample webpage to demonstrate how to implement PHP captcha validation in AJAX contact form or in simple language, we can say how to implement captcha.
Let’s Get Started
First, create a new PHP file and name it as index.php.
Now, write one image tag to show captcha in the contact form.
<form id="contact" action="javascript:void(0)" method="post" onsubmit="submit_form()"> <h3>Contact Form</h3> <fieldset> <img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimg" height="50" width="350"> </fieldset> <fieldset> <input placeholder="Captcha" type="text" tabindex="2" id="captcha_code"> <span class="err captcha-err"> </fieldset> <fieldset> Can't read the above code? <a class="ccc" href="javascript:void(0);" onClick="refresh_captcha();">Refresh</a> </fieldset> <fieldset> <button name="submit" type="submit" id="contact-submit">Submit</button> </fieldset> </form>
Now in the image tag, load a captcha library file that will generate captcha on the contact form using PHP functions.
<img src="captcha.php?rand=?>" id="captchaimg" height="50" width="350">
Take one link to refresh a captcha image, if it isn’t readable to the user.
<a class="ccc" href="javascript:void(0);" onClick="refresh_captcha();">Refresh</a>
After that, it’s time to validate captcha on AJAX call. Create one js file to check whether the captcha is valid or not.
$.ajax({ method: "POST", url: "thank_you.php", data: { captcha: captcha_code} });
Next, create another PHP webpage and name it as thankyou.php
In this page, we’ll check the captcha with session value and make the response with either true or false.
$response = 1; if(empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha']) != 0) { $response = 0; } echo $response; exit;
If it’s successful, it will redirect the user to thank you page with the message.
If not, it will ask you to re-enter the captcha.
Want to Create a Web Application?
Need to validate your app idea or consult with an expert? Get a free consultation now!
Done!
If you face any problem, you can contact our PHP developers for hire.
However, this was just a simple demo. You can build even more advanced contact form by using AJAX contact form builder.
It is designed to build custom contact forms and it’s a light version of AJAX form pro.
Grab a free copy of AJAX Captcha Validation demo from Github.
So, if you want to build an advanced contact form using AJAX for your website, or start a new project in PHP then you can get in touch with us. We are a PHP web development company with 10+ years of experience and have expertise in building scalable web applications in PHP for clients globally.
Apart from the captcha validation you can further improve the security of your web application by Google two-factor authentication login in PHP. Additionally, if you’re looking to improve user engagement on your website, you can explore adding browser push notifications. Check out our blog post on display browser push notification in PHP for a detailed tutorial on how to incorporate this feature into your PHP website.
In case, if you still have any query or confusion regarding PHP captcha form or how to add captcha to PHP form, then you can get in touch with us through our contact us form.