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.
Steps to load dynamic content in bootstrap popup
- Create database
- Database connection
- Create HTML using Bootstrap
- Write jQuery script to open popup
- PHP code to load data in the popup
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # Create orders table CREATE TABLE `customers` ( customer_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, customer_name varchar(255) NOT NULL, customer_email varchar(255) NOT NULL, customer_contact varchar(255) NOT NULL, country varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; # Inserting data in the posts table INSERT INTO `customers` (`customer_id`, `customer_name`, `customer_email`, `customer_contact`, `country`) VALUES |
2. Database connection
We have to create a db_config.php
file and add the following code to connect the database.
1 2 3 4 5 6 7 | <?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $db = "demo"; $con = mysqli_connect($dbhost, $dbuser, $dbpass , $db) or die($con); ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php include("db_config.php"); ?> <!DOCTYPE html> <html> <head> <title>Dynamically load content in Bootstrap Modal with AJAX, PHP MySQL - Clue Mediator</title> <!-- CSS --> <link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' rel='stylesheet' type='text/css'> <!-- Script --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js' type='text/javascript'></script> </head> <body> <div class="container"> <h5 class="mt-3 mb-3">Dynamically load content in Bootstrap Modal with AJAX, PHP MySQL - <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h5> <table class="table table-bordered table-sm"> <thead class="thead-dark"> <tr> <th>Name</th> <th>Email</th> <th></th> </tr> </thead> <tbody> <?php $query = "select * from customers"; $result = mysqli_query($con, $query); while($row = mysqli_fetch_array($result)){ $id = $row['customer_id']; $name = $row['customer_name']; $email = $row['customer_email']; echo "<tr>"; echo "<td>".$name."</td>"; echo "<td>".$email."</td>"; echo "<td><button data-id='".$id."' class='btn btn-info btn-sm btn-popup'>Details</button></td>"; echo "</tr>"; } ?> </tbody> </table> <!-- Modal --> <div class="modal fade" id="custModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title">Customer Details</h4> <button type="button" class="close" data-dismiss="modal">×</button> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </body> </html> |
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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php include("db_config.php"); ?> <!DOCTYPE html> <html> <head> <title>Dynamically load content in Bootstrap Modal with AJAX, PHP MySQL - Clue Mediator</title> ... ... </head> <body> <div class="container"> ... ... </div> <script type="text/javascript"> $(document).ready(function () { $('.btn-popup').click(function () { var custId = $(this).data('id'); $.ajax({ url: 'get_data.php', type: 'post', data: { custId: custId }, success: function (response) { $('.modal-body').html(response); $('#custModal').modal('show'); } }); }); }); </script> </body> </html> |
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
).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php include('db_config.php'); if(isset($_POST['custId'])){ $id = $_POST['custId']; $sql = "select * from customers where customer_id=".$id; $result = mysqli_query($con,$sql); $response = "<table border='0' width='100%'>"; while( $row = mysqli_fetch_array($result) ){ $id = $row['customer_id']; $name = $row['customer_name']; $email = $row['customer_email']; $contact = $row['customer_contact']; $country = $row['country']; $response .= "<tr>"; $response .= "<td>Name : </td><td>".$name."</td>"; $response .= "</tr>"; $response .= "<tr>"; $response .= "<td>Email : </td><td>".$email."</td>"; $response .= "</tr>"; $response .= "<tr>"; $response .= "<td>Contact : </td><td>".$contact."</td>"; $response .= "</tr>"; $response .= "<tr>"; $response .= "<td>Country : </td><td>".$country."</td>"; $response .= "</tr>"; } $response .= "</table>"; echo $response; exit; } ?> |
6. Output
Run the project and check the output in the browser.
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂