PHP - Simple Inline Update Data Using jQuery

In this tutorial we will create a Simple Inline Update Data using jQuery. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_inline, 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_inline");
  3.  
  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="https://sourcecodester.com">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 - Simple Inline Update Data Using jQuery</h3>
  16. <hr style="border-top:1px;"/>
  17. <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Employee</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Gender</th>
  25. <th>Address</th>
  26. <th>Salary</th>
  27. <th>Action</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33.  
  34. $query = mysqli_query($conn, "SELECT * FROM `employee`") or die(mysqli_error());
  35. while($fetch = mysqli_fetch_array($query)){
  36. ?>
  37. <tr>
  38. <td class="editable"><?php echo $fetch['firstname']?></td>
  39. <td class="editable"><?php echo $fetch['lastname']?></td>
  40. <td class="editable"><?php echo $fetch['gender']?></td>
  41. <td class="editable"><?php echo $fetch['address']?></td>
  42. <td class="editable"><?php echo $fetch['salary']?></td>
  43. <td><button class="btn btn-warning edit" type="button" name="<?php echo $fetch['emp_id']?>" id="edit"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>
  44. </tr>
  45. <?php
  46. }
  47. ?>
  48. </tbody>
  49. </table>
  50. </div>
  51. <div id="form_modal" class="modal fade" aria-hidden="true">
  52. <div class="modal-dialog">
  53. <div class="modal-content">
  54. <form method="POST" action="save_employee.php">
  55. <div class="modal-header">
  56. <h3 class="modal-title">Add Employee</h3>
  57. </div>
  58. <div class="modal-bodsy">
  59. <div class="col-md-2"></div>
  60. <div class="col-md-8">
  61. <div class="form-group">
  62. <label>Firstname</label>
  63. <input type="text" class="form-control" name="firstname" required="required"/>
  64. </div>
  65. <div class="form-group">
  66. <label>Lastname</label>
  67. <input type="text" class="form-control" name="lastname" required="required"/>
  68. </div>
  69. <div class="form-group">
  70. <label>Gender</label>
  71. <select name="gender" class="form-control">
  72. <option value="">Select an option</option>
  73. <option value="Male">Male</option>
  74. <option value="Female">Female</option>
  75. </select>
  76. </div>
  77. <div class="form-group">
  78. <label>Address</label>
  79. <input type="text" class="form-control" name="address" required="required"/>
  80. </div>
  81. <div class="form-group">
  82. <label>Salary</label>
  83. <input type="number" min="0" class="form-control" name="salary" required="required"/>
  84. </div>
  85. </div>
  86. </div>
  87. <br style="clear:both;"/>
  88. <div class="modal-footer">
  89. <button type="button" data-dismiss="modal" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Close</button>
  90. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  91. </div>
  92. </form>
  93. </div>
  94. </div>
  95. </div>
  96. <script src="js/jquery-3.2.1.min.js"></script>
  97. <script src="js/bootstrap.js"></script>
  98. <script src="js/script.js"></script>
  99. </body>
  100. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the user data inputs to the MySQLi database server/. To make this just copy and write these block of codes below inside the text editor, then save it as save_employee.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $gender = $_POST['gender'];
  8. $address = $_POST['address'];
  9. $salary = $_POST['salary'];
  10.  
  11. mysqli_query($conn, "INSERT INTO `employee` VALUES('', '$firstname', '$lastname', '$gender', '$address', '$salary')") or die(mysqli_error());
  12.  
  13. header("location: index.php");
  14. }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will update the data in an inline viewing by the use of jQuery design principle. To make this just copy and write these block of codes below inside the text editor, then save it as show below. update.php
  1. <?php
  2. require_once 'conn.php';
  3. $data = $_POST['arrayData'];
  4.  
  5. mysqli_query($conn, "UPDATE `employee` SET `firstname` = '$data[0]', `lastname` = '$data[1]', `gender` = '$data[2]', `address` = '$data[3]', `salary` = '$data[4]' WHERE `emp_id` = '$data[5]'") or die(mysqli_error());
  6.  
  7. echo "Successfully Updated!";
  8. ?>
script.js Note: Make sure to save this file to the js folder to avoid problem in the future.
  1. var type = ["firstname", "lastname", "gender", "address", "salary"];
  2. var count = 0;
  3. $(document).ready(function(){
  4. $('.edit').on('click', function(){
  5. var id = $(this).attr('id');
  6. var name = $(this).attr('name');
  7. if(id === "edit"){
  8. $(this).parents('tr').find('td.editable').each(function(){
  9. var html = $(this).html();
  10. var input = $('<input class="editable" id="'+type[count]+'" type="text" value="'+html+'" size="5"/>');
  11. input.val(html);
  12. $(this).html(input);
  13. count++;
  14. })
  15. $(this).html("<span class='glyphicon glyphicon-save'></span> Update");
  16. $(this).attr('id', 'update');
  17. count=0;
  18. }else if(id==="update"){
  19. var array = new Array();
  20. $(this).parents('tr').find('input').each(function(){
  21. array.push($(this).val());
  22. })
  23.  
  24. array.push(name);
  25. $.ajax({
  26. url: 'update.php',
  27. type: 'POST',
  28. data: {arrayData: array},
  29. success:function(data){
  30. alert(data);
  31.  
  32. window.location = "index.php";
  33. }
  34. });
  35. }
  36. });
  37.  
  38.  
  39. $('.update').on('click', function(){
  40. alert("dsa");
  41. });
  42. });
There you have it we successfully created a Simple Inline Update Data using jQuery. 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