Bootstrap Modal Contact Form with PHP

Last Updated on January 28, 2023 by Roshan Parihar

Bootstrap modal contact form displays a popup on button click that contains the form. It is useful when you don’t want to display the whole form.

You can display only the button to let users click and see the form. After that, they can fill it out and click the submit button to contact them via email.

Bootstrap Modal Contact Form with PHP

A modal is a responsive popup that you can create using Bootstrap. You can place your contact form inside the modal to display it in the popup. It comes with a button or links to click and get the contact form.

The contact form is not required to display everywhere on your website. A simple modal button is enough to collect users’ queries anywhere on the website. Users will click the button and fill out the form from the place where they are.

In this post, you will learn how to create a Bootstrap modal contact form with PHP to send emails. So, let’s get started.

Bootstrap Modal Contact Form with PHP

Here is the step-by-step process on how to create a Bootstrap modal contact form with PHP:-

Step 1: Bootstrap Modal Contact Form Design

To create a contact form design, you can read our post Responsive Contact Form in Bootstrap. You need to follow the 1st step of this post to create a design using Bootstrap.

After that, you have to add the contact form inside the Bootstrap 5 modal. I am going to use the static backdrop Bootstrap modal to create the modal popup.

A static backdrop means users will not be able to close the modal by clicking outside of the popup. They need to click the close ‘X’ button given at the top right corner of the modal to close it. You can collect more leads with this close feature because users will have to see the modal to close it.

Below is the complete code of the Bootstrap modal contact form design:-

When you run the above code in the browser, you will get the Bootstrap modal contact form design as shown in the image below.

Main design

It also contains the loader icon using font awesome icons. When the users submit the form, they will see the rotating icon until they get the result. A rotating loader icon is a confirmation that the form is working backend to give you the result. They will have to wait for some time to get results.

Now, let’s add a jQuery Ajax code to submit the form without a refresh in the next step.

Step 2: jQuery Ajax Submission of Contact Form with Loader Icon

To submit the form without a page refresh, you can use Ajax form submission. You can easily update only the required part of the page using AJax without page refresh.

The below jQuery Ajax code detect the submit event of the form. After submission, it sends the form data to the URL present in the action attribute of the <form> tag. The URL is the PHP file that contains the PHP code to collect and validate form data and send an email when found valid.

The place is indicated in the above design code where you have to place the jQuery Ajax code.

It displays the loader icon immediately after form submission. When the user gets the result, the loader icon automatically hides using the above jQuery code.

Let’s validate the form data in the next step.

Step 3: Get Data and Perform Email Validation Using PHP

To validate the form data, you will have to collect the form data using the $_POST. But, before this, firstly find whether the data is set or not. If the data is not set, don’t allow users to execute the PHP code.

For this, I have added header("location: index.php") to redirect users again to the main page when there is no data available.

When there is some data present, it collects the data and validates the email address with the regular expression you can see below. If the email address is not valid, the PHP code will not allow it to execute further and display an error message that the email is not valid.

Let’s send form data via email when everything is ok in this step.

Step 4: Send Email with PHP Using mail()

When everything is ok, it will move further and can send the form data using the below PHP code. It is self-explanatory with the comments. However, I have also added the description below the code.

Explanation of PHP code to send emails

To understand the above PHP code, read the explanations given below:-

  • $to: Recipient email address who wants to receive emails.
  • $subject: Subject of the email for the contact form.
  • $message: Message or main queries of the customers to collect via email.
  • $headers:
    • ‘From’: The email address of the website owner that should be domain specific to identify where the email comes from.
    • ‘CC’: Other email addresses to receive emails.
    • “Reply-To’: Email address of the user who submits the form.
  • mail(): PHP built-in function to send emails. Pass 4 variables as an argument that is $to, $subject, $message, $headers to pass to be able to send emails on execution.

So, now you know how to create a Bootstrap modal contact form. You also know how to send emails from your form using PHP.

You May Also Like to Read

If you have any queries regarding this post, you can comment below.

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.