PHP Ajax Contact Form with Captcha and Validation

Last Updated on January 22, 2023 by Roshan Parihar

Learn how to create PHP ajax contact form with a captcha and validation. Submit the form to collect users’ data without page refresh using jQuery ajax.

You can easily create a contact form design using Bootstrap pre-defined CSS classes. To use Bootstrap classes, you have to just include its libraries to start using it on your contact form design.

PHP Ajax Contact Form with Captcha and Validation

Bonus Tip

Looking to create contact forms to send emails without page refresh and coding? Create beautiful-looking contact forms with JotForm.

The jQuery ajax contact form validates the users entered code with our captcha code. After the captcha validation success, it sends an email message to the website owner. You will get a success message on each valid entry and an error message on invalid entries.

In this post, I am going to show you how to create a contact form design using Bootstrap and add a captcha to it using PHP.

After that, you will get the jQuery ajax code to submit the form and refresh the captcha code without the page. You will also get PHP code to validate the form data and send emails to the website owner.

So, let’s get started.

PHP Ajax Contact Form with Captcha and Validation

Here is the step-by-step process to help you easily create a PHP Ajax contact form with a captcha:-

Step 1: PHP Captcha Image Creation

The first step is to create a captcha image of code using PHP to add to your contact form. To create a captcha image code, you have to start a session to store the captcha code. After that, create a randomly generated 6-digit captcha code using rand() with md5 encryption.

The below PHP code will generate a captcha image when you run it on your browser. Each time you refresh this page on your browser, you will get a different captcha code.

The code contains useful comments to easily understand the use of each code and learn to create the captcha code using PHP.

Now, your captcha code is ready. Let’s create a beautiful contact form design using Bootstrap 5 and add this captcha code image to it.

Step 2: Create Contact Form Design Using Bootstrap 5 with Captcha Image

Bootstrap is the mobile-first and most useful responsive framework. You can use it to easily create mobile-friendly contact form designs for your website.

To start using Bootstrap 5 classes work for you, you have to include its style CDN URL as given below. You have to include it in the <head> section of your HTML contact form design code.

We are also going to use font awesome icons and jQuery script. To font awesome icons and jQuery script work for you, you have to include the below CDN URLs to the <head> section.

Now, after adding the above CDN URLs, you can create your contact form design and make the captcha code work. Below is the complete code of the final design of the contact form with the captcha code.

You can use the below code to get the exact design whose image is shown after the below code.

The contact form contains all the important fields useful to make communicate with your audience. It also contains the captcha code to validate with the user’s entered code. Captcha is an important part of contact forms to stop spam bots fill them.

Design

Let’s add a jQuery ajax script to submit the form without page refresh. The script can also refresh the jQuery ajax code.

Step 3: jQuery Ajax Contact Form Submission with Captcha Code Refresh

jQuery ajax is useful to submit the form and update some parts of the page without page refresh after form submission. You have to place the below code just before the </body> close tag to make it work on your contact form.

To refresh the captcha, the below script uses the PHP file ‘captcha.php’ that contains the captcha image generation script. As the captcha code is an image, you have to use jQuery attr() to update the source of the image as given below.

The below ajax submission script sends form data to the PHP file ‘send-email.php’. This file contains the PHP script to validate form data and captcha image code. If the data and captcha code is showing valid, it allows submission of the form to send emails to the owner of the website.

The jQuery ajax code sends the form data in serialization and submits the form with the post method. You will also get notification messages below the form after the successful submission of the form.

Step 4: PHP Code to Send Emails with Captcha Validation

To make PHP ajax contact form work, you have to add the simple PHP code given below. It first collects all the form data including the captcha code. As we have made all the input required using the HTML inbuilt required attribute. So, there is no need to manually make fields required.

However, the only thing we have to validate is the email address and the captcha code to match the users entered email and captcha code. The below code contains the email validation to get the correct email address from your users.

After email validation, the code checks the captcha code if matching with the user’s entered captcha code in the input box. When it found the captcha code matching with the users entered code, the next PHP code will execute to send data in an email to the receiver.

You will also get an error and success message designed using Bootstrap 5. The success message will display in green color while the error message will display in red color.

That’s all about PHP ajax contact form.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.