Load dynamic content in Bootstrap Modal with AJAX, PHP and MySQL

Today, we’ll explain to you how to load dynamic content in Bootstrap Modal with AJAX, PHP and MySQL.

The model is used to display a popup window or dialog box on the current web page. It is used for display form, image, lightboxes, notifications, information, terms and conditions etc. Implementing the Bootstrap popup is very easy because no third party jQuery plugin is required if you use Bootstrap in your web application.

In the previous article, we have learned Create a Modal Popup using jQuery without bootstrap.

Demo Example

Here, we will show the customers with the details button in the table. When the button is clicked fetch all customer’s details from the database and will display in the popup.

Output - Load dynamic content in Bootstrap Modal with AJAX, PHP and MySQL - Clue Mediator
Output – Load dynamic content in Bootstrap Modal with AJAX, PHP and MySQL – Clue Mediator

Steps to load dynamic content in bootstrap popup

  1. Create database
  2. Database connection
  3. Create HTML using Bootstrap
  4. Write jQuery script to open popup
  5. PHP code to load data in the popup
  6. Output

File Structure

  • load-dynamic-content-bootstrap-popup-php
    • db_config.php
    • get_data.php
    • index.php

1. Create database

First, we will create a table named customers in the demo database. Run the following script to create a table with dummy records in the database.

2. Database connection

We have to create a db_config.php file and add the following code to connect the database.

db_config.php

3. Create HTML using Bootstrap

Let’s create an HTML in the index.php file to list out the customer data in the table using bootstrap library.

On this page, when the Details button is clicked, the customer_id will be sent to the get_data.php file via AJAX request. The PHP script will retrieve the records from the database as per request id and return an HTML to load dynamically into the modal window.

index.php

4. Write jQuery script to open popup

Let’s add the following code in the index.php file that sends the customer id to the server-side script via Ajax request when the Details button is clicked.

Get the response from the get_data.php file and display the HTML data to the div element with class modal-body.

index.php

5. PHP code to load data in the popup

At last, we will create a file named get_data.php. The Ajax request is sent to this PHP file and then fetches the customer record from the database based on the request id (custId).

get_data.php

6. Output

Run the project and check the output in the browser.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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