Delete Data In PHP & MYSQL without Refreshening Page

0
240

This tutorial is all about Delete Data In PHP & MYSQL without Refreshening Page. In this tutorial you will learn on Delete Data In PHP & MYSQL without Refreshening Page.

Good Day! Everyone! Today I’m going to teach you my upgradable version in deleting data in your database. Unlike with my previous tutorial which is about a very simple way in deleting data in database. Here, you don’t need to refresh your page in order to delete data in your database and we add some styles now.

So before you go with this tutorial, for the beginners, I suggest to start with my previous tutorial regarding simple way of inserting, deleting and displaying data in PHP. >> click here << to go my previous article.

So now, we will going to start our tutorial.

Make sure you already have the following. You can download it below:

bootstrap.min

bootstrap.min

jquery-3.2.1.min

Now, if you are already done downloading all the files below. Just go now to your browser then go to PHPMYADMIN to create a database.

Create a database then name it as any name you desire. In my case, I use “itsourcecode” as the name of the database

Then create a table. Put “mydata” as the table name then put the following attributes.

  1. CREATE TABLE `mydata` ( `id` int(11) NOT NULL, `name` text NOT NULL, `age` int(11) NOT NULL, `gender` text NOT NULL, `birthday` date NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now for the connection to the database. Create a “connection.php” file then put the following codes.

  1. &lt;?php
  2. $servername = 'localhost';
  3. $username = 'root';
  4. $password = '';
  5. $dbname = 'itsourcecode';
  6.  
  7. try {
  8. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  9. $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. } catch (Exception $ex) {
  11. echo "Connection Failed: ". $ex-&gt;getMessage();
  12. }
  13. ?&gt;

As you can see, we now upgraded our coding structure in managing our database using PDO. This is now the updated version in MYSQL.

Now, create a “index.php” file then put the following codes.

  1. Deleting Data Without Refreshing the Page
  2.  
  3. <a href="http://assets/js/jquery-3.2.1.min.js">http://assets/js/jquery-3.2.1.min.js</a>
  4. <a href="http://assets/js/bootstrap.min.js">http://assets/js/bootstrap.min.js</a>
  5. <div class="container">
  6. <h5>Deleting Data in PHP/MYSQL Without Refreshing the Page</h5>
  7. &nbsp;
  8. <div class="row">
  9. <div class="col-sm-3"><button class="btn btn-info btn-md" type="button">Add Data</button></div>
  10. </div>
  11. <div class="row">
  12. <table id="itemtable" class="table table-condensed">
  13. <thead>
  14. <tr>
  15. <th>Name</th>
  16. <th>Age</th>
  17. <th>Gender</th>
  18. <th>Birthday</th>
  19. <th>Action</th>
  20. </tr>
  21. </thead>
  22. <tbody id="dataBody"></tbody>
  23. </table>
  24. </div>
  25. </div>
  26. <!-- Start Add Data Modal -->
  27. <div id="addDataModal" class="modal fade">
  28. <div class="modal-dialog">
  29. <div class="modal-content">
  30. <div class="modal-header"><button class="close" type="button">x</button>
  31. <h4 class="modal-title">Add New Data</h4>
  32. </div>
  33. <form id="add-form" class="form-horizontal">
  34. <div class="modal-body">
  35. <div class="form-group"><label class="control-label col-sm-2" for="name">Name:</label>
  36. <div class="col-sm-10"></div>
  37. </div>
  38. <div class="form-group"><label class="control-label col-sm-2" for="age">Age:</label>
  39. <div class="col-sm-3"></div>
  40. </div>
  41. <div class="form-group"><label class="control-label col-sm-2" for="gender">Gender:</label>
  42. <div class="col-sm-3">
  43.  
  44. -- Gender --
  45. Male
  46. Female
  47.  
  48. </div>
  49. </div>
  50. <div class="form-group"><label class="control-label col-sm-2" for="bday">Birthday:</label>
  51. <div class="col-sm-4"></div>
  52. </div>
  53. </div>
  54. <div class="modal-footer"><button class="btn btn-primary" type="submit">Add</button>
  55. <button class="btn btn-danger" type="button">Close</button></div>
  56. </form></div>
  57. </div>
  58. </div>
  59. <!-- End Add Data Modal -->
  60.  
  61. <!-- Start Delete Data Modal -->
  62. <div id="deleteModal" class="modal fade">
  63. <div class="modal-dialog">
  64. <div class="modal-content">
  65. <div class="modal-header"><button class="close" type="button">x</button>
  66. <h4 class="modal-title">Delete Data</h4>
  67. </div>
  68. <div class="modal-body">
  69.  
  70. Are you sure you want to delete this data?
  71.  
  72. </div>
  73. <div class="modal-footer"><button class="btn btn-danger yes" type="button">Yes</button>
  74. <button class="btn btn-primary" type="button">No</button></div>
  75. </div>
  76. </div>
  77. </div>
  78. <!-- End Delete Data Modal -->
  79.  
  80. // When the HTML or documents loads, Load datas in table.
  81. $(document).ready(function(){
  82. function showData() {
  83. $.ajax({
  84. url: 'dataFunctions.php',
  85. type: 'POST',
  86. data: {action : 'showData'},
  87. dataType: 'html',
  88. success: function(result)
  89. {
  90. $('#dataBody').html(result);
  91. },
  92. error: function()
  93. {
  94. alert('Failed!');
  95. }
  96. })
  97. }
  98. showData();
  99. });
  100.  
  101. //Add form submit handler
  102. $('#add-form').submit(function(e){
  103. e.preventDefault();
  104. $.ajax({
  105. url: 'dataFunctions.php',
  106. type: 'POST',
  107. data: $(this).serialize(),
  108. dataType: 'html',
  109. success: function(result)
  110. {
  111. $('#dataBody').html(result);
  112. $('#addDataModal').modal('toggle');
  113. },
  114. error: function()
  115. {
  116. alert('Failed!');
  117. }
  118. })
  119. });
  120.  
  121. function deleteData(id) {
  122. $('#deleteModal').modal('show');
  123. $('.yes').click(function(e){
  124. $.ajax({
  125. url: 'dataFunctions.php',
  126. type: 'POST',
  127. data: {action : 'deleteData', id : id},
  128. dataType: 'html',
  129. success: function(result)
  130. {
  131. $('#dataBody').html(result);
  132. $('#deleteModal').modal("hide");
  133. }
  134. })
  135. })
  136. }

Then create a “dataFunctions.php” file then put the following codes. These are the functions in manipulating data in our database.

Facebook Comments
(Visited 255 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here