The Modal is a popup window or dialog box that displayed over the current web page. It is very useful to display HTML content/elements on a single page. If your web application uses Bootstrap, the modal popup can be easily implemented on the web pages. Bootstrap’s modal plugin helps to add a dialog window to the website site for lightboxes, popup elements, or custom content.
Creating a modal popup is very easy with the Bootstrap modal component. So, if your web application already uses Bootstrap, itβs always a good idea to use Bootstrap for populating modal dialog. Because it does not require any third-party jQuery plugin. Not only the static content but also you can load external URL or dynamic content in a modal popup with Bootstrap.
In this tutorial, we will show how you can load content from an external URL in Bootstrap modal popup. Also, you will know how to load dynamic content from another page via jQuery Ajax and display it in Bootstrap modal popup. Using our example script, you can pass data, variables, or parameters to the external URL and get dynamic content via jQuery Ajax.
Before using the Bootstrap to create a modal popup, include the Bootstrap and jQuery library first.
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Bootstrap library -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
The following HTML contains a button and a modal dialog. This button (.openBtn
) triggers the Bootstrap modal for showing the content from another file.
<!-- Trigger the modal with a button --> <button type="button" class="btn btn-success openBtn">Open Modal</button> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Modal with Dynamic Content</h4> </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>
This example shows how to load the content from an external URL in the Bootstrap modal popup.
JavaScript Code:
By clicking the Open Modal (.openBtn
) button, the content is loaded from another page (content.html
) and shows on the modal popup (#myModal
).
<script>
$('.openBtn').on('click',function(){
$('.modal-body').load('content.html',function(){
$('#myModal').modal({show:true});
});
});
</script>
This example shows how to load the dynamic content based on parameter pass into the external URL using PHP and MySQL.
JavaScript Code:
By clicking the Open Modal (.openBtn
) button, the dynamic content is loaded from another PHP file (getContent.php
) based on the ID and shows on the modal popup (#myModal
).
<script>
$('.openBtn').on('click',function(){
$('.modal-body').load('getContent.php?id=2',function(){
$('#myModal').modal({show:true});
});
});
</script>
Dynamic Data using PHP & MySQL:
In the getContent.php
file, the requested data is fetched from the database using PHP and MySQL. The dynamic content is rendered and return to the Bootstrap modal.
<?php
if(!empty($_GET['id'])){
// Database configuration
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbName = 'codexworld';
// Create connection and select database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if ($db->connect_error) {
die("Unable to connect database: " . $db->connect_error);
}
// Get content from the database
$query = $db->query("SELECT * FROM cms_content WHERE id = {$_GET['id']}");
if($query->num_rows > 0){
$cmsData = $query->fetch_assoc();
echo '<h5>'.$cmsData['title'].'</h5>';
echo '<p>'.$cmsData['content'].'</p>';
}else{
echo 'Content not found....';
}
}else{
echo 'Content not found....';
}
?>
This example shows how you can load dynamic content from an external URL in the Bootstrap modal.
HTML Code:
In the data-href
attribute, the URL needs to be specified, from where you want to load the content. Also, add the openPopup class to the link to trigger the Bootstrap modal dialog.
<a href="javascript:void(0);" data-href="getContent.php?id=1" class="openPopup">About Us</a> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Bootstrap Modal with Dynamic Content</h4> </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>
JavaScript Code:
The following jQuery automatically loads the content from the specified URL in the data-href attribute and opens the Bootstrap modal with this dynamic content.
<script>
$(document).ready(function(){
$('.openPopup').on('click',function(){
var dataURL = $(this).attr('data-href');
$('.modal-body').load(dataURL,function(){
$('#myModal').modal({show:true});
});
});
});
</script>
Bootstrap Modal Popup Form Submit with Ajax & PHP
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
my code looks like this
$(‘.openBtn’).on(‘click’,function(){
var id = $(this).attr(‘data-id’);
alert (“Made it here: Variable is = “+id);
$(‘.modal-body’).load(‘fetch_record.php?rowid=’+id,function(){
$(‘#myModal’).modal({show:true});
});
});
Ah, the CSS is being overridden by the “bootstrap-4.4.1.css” :(. I probably need these to be restyled in my CSS or apply a separate class.
This is great, although the modal text is much larger than the original HTML (I’m linking to a simple HTML page on the same site). CSS seems to be applied correctly to the modal, but heders are massive (for example).
Thank you so much. It Really helped me a lot.
Hello, how i can reload the principal page when i close modal windows?
very thank
Thanks for this one. =)
but ive got one question.
whta if ive got multiple anchor buttons on a table and it is looped whith the same class but have different data-href? how can i get the value of the data-href?
Thanks in advance =)
How i control width and height of the modal window?
WOh Thanks for updating it CodexWorld π I’ve used it on my project π
Wohh. Thanks CodexWorld for Updating π
Hello CodexWorld. Can you make a Version for Codeigniter?? And how about the id is not 2 how can identify different ID’s?
We have updated the tutorial, see the last section, it will fulfill your requirement.
This one is cool π Thanks CodexWorld… More Tutorials and More Power
Great tuto, I’ll implement it on my website. But I’m planning migrate from Bootstrap to Bulma, I have seen some improvements that I like more. Thanks!