Bootstrap Modal Popup Form Submit with Ajax & PHP


If your web application uses Twitter Bootstrap framework, modal popup integration is very easy. Bootstrap helps you to add a popup to button or link without using additional JavaScript code. Also, using Bootstrap, you can build a well-designed HTML form in less time.

This tutorial will show how you can integrate a modal popup form to your website using Bootstrap and submit the form with jQuery, Ajax, and PHP. For example, we’ll build a contact form with Bootstrap modal popup and submit the form after validation using jQuery, Ajax, and PHP. The following functionality will be implemented in Bootstrap modal form script.

  • Modal popup with contact form using Bootstrap.
  • Validate form data before submit using jQuery.
  • Submit form data with jQuery, Ajax, and PHP.
  • Send the email to website admin using PHP.

Bootstrap & jQuery Library

Bootstrap is used to create modal popup and design HTMl form, include the bootstrap and jQuery library first.

<!-- Latest minified bootstrap css -->
<link rel="stylesheet" href="">

<!-- jQuery library -->
<script src=""></script>

<!-- Latest minified bootstrap js -->
<script src=""></script>

HTML Code: Bootstrap Modal Popup Form

The following HTML creates a dialog popup window using bootstrap. A button is used to trigger this modal window and open a form for submitting the contact request. The button or link needs two data-* attributes, data-toggle="modal" and data-target="#modalForm". Also, the modal div must have and id attribute that should be the same as data-target attribute of trigger link or button.

<!-- Button to trigger modal -->
<button class="btn btn-success btn-lg" data-toggle="modal" data-target="#modalForm">
    Open Contact Form

<!-- Modal -->
<div class="modal fade" id="modalForm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                <h4 class="modal-title" id="myModalLabel">Contact Form</h4>
            <!-- Modal Body -->
            <div class="modal-body">
                <p class="statusMsg"></p>
                <form role="form">
                    <div class="form-group">
                        <label for="inputName">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Enter your name"/>
                    <div class="form-group">
                        <label for="inputEmail">Email</label>
                        <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email"/>
                    <div class="form-group">
                        <label for="inputMessage">Message</label>
                        <textarea class="form-control" id="inputMessage" placeholder="Enter your message"></textarea>
            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary submitBtn" onclick="submitContactForm()">SUBMIT</button>

JavaScript Code: Validate and Submit Form

The submitContactForm() function is triggered on clicking the form submit button. In the submitContactForm() function, popup form data is validated before submit and submitted to the submit_form.php file for further processing using jQuery and Ajax.

function submitContactForm(){
    var reg = /^[A-Z0-9._%+-][email protected]([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
    var name = $('#inputName').val();
    var email = $('#inputEmail').val();
    var message = $('#inputMessage').val();
    if(name.trim() == '' ){
        alert('Please enter your name.');
        return false;
    }else if(email.trim() == '' ){
        alert('Please enter your email.');
        return false;
    }else if(email.trim() != '' && !reg.test(email)){
        alert('Please enter valid email.');
        return false;
    }else if(message.trim() == '' ){
        alert('Please enter your message.');
        return false;
            beforeSend: function () {
                $('.modal-body').css('opacity', '.5');
                if(msg == 'ok'){
                    $('.statusMsg').html('<span style="color:green;">Thanks for contacting us, we\'ll get back to you soon.</p>');
                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                $('.modal-body').css('opacity', '');

Send Contact Request Email (submit_form.php)

In the submit_form.php file, the following works are done for processing the form submit request.

  • Check whether submitted form is not empty and validate email by FILTER_VALIDATE_EMAIL filter in PHP.
  • Get the form data using PHP $_POST method.
  • Send HTML email with contact details to site admin using PHP mail() function.

Once the required action is completed, the status message is rendered that would be used by the Ajax success method.

if(isset($_POST['contactFrmSubmit']) && !empty($_POST['name']) && !empty($_POST['email']) && (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) && !empty($_POST['message'])){
// Submitted form data
$name   $_POST['name'];
$email  $_POST['email'];
     * Send email to admin
$to     '[email protected]';
$subject'Contact Request Submitted';
$htmlContent '
    <h4>Contact request has submitted at semicolonworld, details are given below.</h4>
    <table cellspacing="0" style="width: 300px; height: 200px;">
        <tr style="background-color: #e0e0e0;">
// Set content-type header for sending HTML email
$headers "MIME-Version: 1.0" "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" "\r\n";
// Additional headers
$headers .= 'From: semicolonworld<[email protected]>' "\r\n";
// Send email
$status 'ok';
$status 'err';
// Output status
echo $status;die;

Related Articles

Comments 0