PHP - Passing Data To Bootstrap Modal

In this tutorial we will create a Passing Data To Bootstrap Modal using PHP. This code can pass mysqli data to bootstrap modal when user click the button. The code use MySQLi SELECT query to display a row of data from the table that has binded by an id in the button in order to pass the data to modal dialog. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_pass, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_pass");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Passing Data To Bootstrap Modal</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-8">
  18. <table class="table table-bordered">
  19. <thead class="alert-info">
  20. <tr>
  21. <th>Firstname</th>
  22. <th>Lastname</th>
  23. <th>Address</th>
  24. <th>Action</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <?php
  29. require'conn.php';
  30. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  31. while($fetch=mysqli_fetch_array($query)){
  32. ?>
  33. <tr>
  34. <td><?php echo $fetch['firstname']?></td>
  35. <td><?php echo $fetch['lastname']?></td>
  36. <td><?php echo $fetch['address']?></td>
  37. <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal<?php echo $fetch['mem_id']?>">Open Modal</button></td>
  38. </tr>
  40. <div class="modal fade" id="form_modal<?php echo $fetch['mem_id']?>" aria-hidden="true">
  41. <div class="modal-dialog">
  42. <div class="modal-content">
  43. <div class="modal-header">
  44. <h3 class="modal-title">User Details</h3>
  45. </div>
  46. <div class="modal-body">
  47. <div class="col-md-2"></div>
  48. <div class="col-md-8">
  49. <div class="form-group">
  50. <label>Firstname</labe/>
  51. <input type="text" class="form-control" value="<?php echo $fetch['firstname']?>" disabled="disabled"/>
  52. </div>
  53. <div class="form-group">
  54. <label>Lastname</labe/>
  55. <input type="text" class="form-control" value="<?php echo $fetch['lastname']?>" disabled="disabled"/>
  56. </div>
  57. <div class="form-group">
  58. <label>Gender</labe/>
  59. <input type="text" class="form-control" value="<?php echo $fetch['gender']?>" disabled="disabled"/>
  60. </div>
  61. <div class="form-group">
  62. <label>Age</labe/>
  63. <input type="number" class="form-control" value="<?php echo $fetch['age']?>" disabled="disabled"/>
  64. </div>
  65. <div class="form-group">
  66. <label>Address</labe/>
  67. <input type="text" class="form-control" value="<?php echo $fetch['address']?>" disabled="disabled"/>
  68. </div>
  69. </div>
  70. </div>
  71. <div style="clear:both;"></div>
  72. <div class="modal-footer">
  73. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. <?php
  79. }
  80. ?>
  81. </tbody>
  82. </table>
  83. </div>
  84. <div class="col-md-4">
  85. <form method="POST" action="insert.php">
  86. <div class="form-group">
  87. <label>Firstname</label>
  88. <input type="text" class="form-control" name="firstname" required="required"/>
  89. </div>
  90. <div class="form-group">
  91. <label>Lastname</label>
  92. <input type="text" class="form-control" name="lastname" required="required"/>
  93. </div>
  94. <div class="form-group">
  95. <label>Gender</label>
  96. <select name="gender" class="form-control" required="required">
  97. <option value="Male">Male</option>
  98. <option value="Female">Female</option>
  99. </select>
  100. </div>
  101. <div class="form-group">
  102. <label>Age</label>
  103. <input type="number" class="form-control" name="age" required="required"/>
  104. </div>
  105. <div class="form-group">
  106. <label>Address</label>
  107. <input type="text" class="form-control" name="address" required="required"/>
  108. </div>
  110. <center><button class="btn btn-primary" name="insert">Insert</button></center>
  111. </form>
  112. </div>
  113. </div>
  114. <script src="js/jquery-3.2.1.min.js"></script>
  115. <script src="js/bootstrap.js"></script>
  116. </body>
  117. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the form inputs to the MySQLi server. To do that just copy and write this block of codes inside the text editor, then save it as insert.php.
  1. <?php
  2. require_once'conn.php';
  4. if(ISSET($_POST['insert'])){
  5. $firstname=$_POST['firstname'];
  6. $lastname=$_POST['lastname'];
  7. $gender=$_POST['gender'];
  8. $age=$_POST['age'];
  9. $address=$_POST['address'];
  11. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$gender', '$age', '$address')") or die(mysqli_error());
  12. header("location: index.php");
  13. }
  14. ?>
There you have it we successfully created Passing Data To Bootstrap Modal using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment