Insert and Display Data in PHP & MYSQL Without Refreshening Page

1
486

This tutorial is all about Insert and Display Data in PHP & MYSQL Without Refreshening Page. In this tutorial you will learn Insert and Display Data in PHP & MYSQL Without Refreshening Page.

Good Day! Everyone! Today I’m going to teach you my inserting and displaying data in your database. Unlike with my previous tutorial which is about a very simple way in inserting and showing data in database. Here, you don’t need to refresh your page in order to display the inserted data 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 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:

jquery-3.2.1.min

bootstrap.min

bootstrap.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 <code>mydata</code> (<code>id</code> int(11) NOT NULL, <code>name</code> text NOT NULL, <code>age</code> int(11) NOT NULL, <code>gender</code> text NOT NULL, <code>birthday</code> 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. date_default_timezone_set('Asia/Manila');
  3. $servername = 'localhost';
  4. $username = 'root';
  5. $password = '';
  6. $dbname = 'itsourcecode';
  7.  
  8. try {
  9. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  10. $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. } catch (Exception $ex) {
  12. echo "Connection Failed: ". $ex-&gt;getMessage();
  13. }
  14. ?&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. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;meta charset="UTF-8"&gt;
  5. &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  6. &lt;title&gt;Inserting Data Without Refreshing the Page&lt;/title&gt;
  7. &lt;link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"/&gt;
  8. <a href="http://assets/js/jquery-3.2.1.min.js">http://assets/js/jquery-3.2.1.min.js</a>
  9. <a href="http://assets/js/bootstrap.min.js">http://assets/js/bootstrap.min.js</a>
  10. &lt;/head&gt;
  11. &lt;body&gt;
  12. <div class="container">
  13. <h5>Inserting Data in PHP/MYSQL Without Refreshing the Page</h5><br>
  14. <div class="row">
  15. <div class="col-sm-3">
  16. <button type="button" class="btn btn-info btn-md">Add Data</button>
  17. </div>
  18. &lt;/div&gt;
  19. <div class="row">
  20. <table id="itemtable" class="table table-condensed">
  21. <thead>
  22. <tr>
  23. <th>Name</th>
  24. <th>Age</th>
  25. <th>Gender</th>
  26. <th>Birthday</th>
  27. </tr>
  28. </thead>
  29. <tbody id="dataBody"></tbody>
  30. </table>
  31. </div>
  32. &lt;/div&gt;
  33.  
  34. &lt;!-- Start Add Data Modal --&gt;
  35. <div id="addDataModal" class="modal fade" role="dialog">
  36. <div class="modal-dialog">
  37. <div class="modal-content">
  38. <div class="modal-header">
  39. <button type="button" class="close">x</button>
  40. <h4 class="modal-title">Add New Data</h4>
  41. </div>
  42. &lt;form class="form-horizontal" id="add-form"&gt;
  43. <div class="modal-body">
  44. <div class="form-group">
  45. <label class="control-label col-sm-2" for="name">Name:</label>
  46. <div class="col-sm-10">
  47.  
  48. </div>
  49. &lt;/div&gt;
  50. <div class="form-group">
  51. <label class="control-label col-sm-2" for="age">Age:</label>
  52. <div class="col-sm-3">
  53.  
  54. </div>
  55. &lt;/div&gt;
  56. <div class="form-group">
  57. <label class="control-label col-sm-2" for="gender">Gender:</label>
  58. <div class="col-sm-3">
  59.  
  60. -- Gender --
  61. Male
  62. Female
  63.  
  64. </div>
  65. &lt;/div&gt;
  66. <div class="form-group">
  67. <label class="control-label col-sm-2" for="bday">Birthday:</label>
  68. <div class="col-sm-4">
  69.  
  70. </div>
  71. &lt;/div&gt;
  72. &lt;input type="hidden" name="action" value="addData"&gt;
  73. &lt;/div&gt;
  74. <div class="modal-footer">
  75. <button type="submit" class="btn btn-primary">Add</button>
  76. <button type="button" class="btn btn-danger">Close</button>
  77. </div>
  78. &lt;/form&gt;
  79. &lt;/div&gt;
  80. &lt;/div&gt;
  81. &lt;/div&gt;
  82. &lt;!-- End Add Data Modal --&gt;
  83.  
  84.  
  85. // When the HTML or documents loads, Load datas in table.
  86. $(document).ready(function(){
  87. function showData() {
  88. $.ajax({
  89. url: 'dataFunctions.php',
  90. type: 'POST',
  91. data: {action : 'showData'},
  92. dataType: 'html',
  93. success: function(result)
  94. {
  95. $('#dataBody').html(result);
  96. },
  97. error: function()
  98. {
  99. alert('Failed!');
  100. }
  101. })
  102. }
  103. showData();
  104. });
  105.  
  106. //Add form submit handler
  107. $('#add-form').submit(function(e){
  108. e.preventDefault();
  109. $.ajax({
  110. url: 'dataFunctions.php',
  111. type: 'POST',
  112. data: $(this).serialize(),
  113. dataType: 'html',
  114. success: function(result)
  115. {
  116. $('#dataBody').html(result);
  117. $('#addDataModal').modal('toggle');
  118. },
  119. error: function()
  120. {
  121. alert('Failed!');
  122. }
  123. })
  124. });
  125.  
  126. &lt;/body&gt;
  127. &lt;/html&gt;

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

  1. &lt;?php
  2. if (isset($_POST['action']) &amp;&amp; !empty($_POST['action'])) {
  3. $action = $_POST['action'];
  4. switch($action) {
  5. case 'addData':
  6. addData();
  7. break;
  8. case 'showData':
  9. displayData();
  10. break;
  11. default:
  12. # code...
  13. break;
  14. }
  15. }
  16.  
  17. function addData() {
  18. include 'connection.php';
  19.  
  20. $stmt = $conn-&gt;prepare("INSERT INTO mydata (name, age, gender, birthday) VALUES (:name, :age, :gender, :birthday)");
  21.  
  22. $name = $_POST['name'];
  23. $age = $_POST['age'];
  24. $gender = $_POST['gender'];
  25. $birthday = $_POST['bday'];
  26.  
  27. $stmt-&gt;bindParam(':name', $name);
  28. $stmt-&gt;bindParam(':age', $age);
  29. $stmt-&gt;bindParam(':gender', $gender);
  30. $stmt-&gt;bindParam(':birthday', $birthday);
  31. $stmt-&gt;execute();
  32.  
  33. displayData();
  34. }
  35.  
  36. function displayData() {
  37. include 'connection.php';
  38.  
  39. $data = $conn-&gt;query("SELECT * FROM mydata");
  40.  
  41. while ($r = $data-&gt;fetch()) {
  42. echo '&lt;tr&gt;';
  43. echo '&lt;td&gt;' . $r['name'] . '&lt;/td&gt;';
  44. echo '&lt;td&gt;' . $r['age'] . '&lt;/td&gt;';
  45. echo '&lt;td&gt;' . $r['gender'] . '&lt;/td&gt;';
  46. echo '&lt;td&gt;' . $r['birthday'] . '&lt;/td&gt;';
  47. echo '&lt;/tr&gt;';
  48. }
  49. }
  50. ?&gt;

Screenshots:

 

 

If you have questions regarding my tutorial which is entitled as “INSERT AND DISPLAY DATA IN PHP & MYSQL WITHOUT REFRESHING PAGE” feel free to ask us by commenting below or by visiting on our contact page. Thank you for supporting me and ITSOURCECODE.COM.

Facebook Comments

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here