In this tutorial you will see the dynamic bootstrap modal popup loading dynamic content through MYSQL database.
First create a table named Post in your MYSQL database and add these fields:-
- id
- post_title
- created_date
- author_name
- description
and then add some records in the table.
Now We need to create three php files to get dynamic bootstrap modal:-
- config.php -In this file we will write the code to make connection with the database.
- modal.php – In this file we will write a code to show the post title and view more button which triggers the dynamic modal popup.
- ajax.php – This file will receive a unique id when user clicks on any post’s view more button and using that id we will fetch the content from database related to that post and send the content to modal.php file to show that content in the modal body.
Config.php
1 2 3 |
<?php $con=mysqli_connect("localhost","root","","post_database"); ?> |
Modal.php
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<html> <title>Dynamic Modal</title> <head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> </head> <body> <?php require_once('config.php'); $query="select * from posts"; $execute=mysqli_query($con,$query); ?> <table class="table text-center"> <thead class="thead-dark"> <tr> <th scope="col">#</th> <th scope="col">Post Title</th> <th>More Details</th> </tr> </thead> <tbody> <?php while($row=mysqli_fetch_assoc($execute)) { ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['title']; ?></td> <td><button type="button" class="btn btn-primary modalButton" data-toggle="modal" data-id="<?php echo $row['id'];?>"> View More </button> </td> </tr> <?php } ?> </tbody> </table> <!--Modal starts Here--> <div class="modal fade" id="dynamicModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div> </div> </div> </body> <script> $(document).ready(function(){ $(".modalButton").click(function(){ var id =$(this).data('id'); $.ajax({ url:"ajax.php", method:"post", data:{id:id}, success:function(response){ $(".modal-body").html(response); $("#dynamicModal").modal('show'); } }) }) }) </script> </html> |
Ajax.php
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 |
<?php require_once('config.php'); $id=$_POST['id']; $query='select * from posts where id="'.$id.'"'; $execute=mysqli_query($con,$query); $result=mysqli_fetch_assoc($execute); $author=$result['author']; $createdAt=$result['created_at']; $description=$result['body']; $response='<form> <div class="form-group"> <label>Created Date</label> <input type="text" class="form-control" value="'.$createdAt.'" readonly> </div> <div class="form-group"> <label>Author Name</label> <input type="text" class="form-control" value="'.$author.'" readonly> </div> <div class="form-group"> <label>Description</label> <textarea rows="4" cols="50" class="form-control" readonly>'.$description.'</textarea> </div> </form>'; echo $response; ?> |
awesome guildline
Thanks for share this Code
Finally I got the code. Thanks,I needed it urgently.
Thank you for sharing such a simple code . It saved my lot of time.