PHP - Simple Table Data Highlighting using jQuery

Submitted by: 
Language: 
Visitors have accessed this post 288 times.

In this tutorial we will create a Simple Table Data Highlighting using jQuery. This code will highlight a specific row or column base on the mouse pointer position.The code itself use jQuery script to modify the css content of each element dynamically. This is a user-friendly kind of program feel free user it in your program.

We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each properties of an element based on different criteria such as id, class, etc.

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_highlight, 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_highlight");
  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. <style>
  7. .highlight-row {
  8. background-color:maroon;
  9. cursor:pointer;
  10. color:#FFF;
  11. }
  12. .highlight-col {
  13. background-color:indigo;
  14. color:#FFF;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <nav class="navbar navbar-default">
  20. <div class="container-fluid">
  21. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  22. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  23. </a> </div>
  24. </nav>
  25. <div class="col-md-3"></div>
  26. <div class="col-md-6 well">
  27. <h3 class="text-primary">PHP - Simple Table Data Highlighting using jQuery</h3>
  28. <hr style="border-top:1px dotted #ccc;"/>
  29. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add User</button>
  30. <br /><br />
  31. <table class="table table-bordered">
  32. <thead class="alert-info">
  33. <tr>
  34. <th class="header">Firstname</th>
  35. <th class="header">Lastname</th>
  36. <th class="header">Address</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <?php
  41. require 'conn.php';
  42.  
  43. $query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  44. while($fetch = mysqli_fetch_array($query)){
  45. ?>
  46. <tr class="table-row">
  47. <td><?php echo $fetch['firstname']?></td>
  48. <td><?php echo $fetch['lastname']?></td>
  49. <td><?php echo $fetch['address']?></td>
  50. </tr>
  51. <?php
  52. }
  53. ?>
  54. </tbody>
  55. </table>
  56. </div>
  57. <div class="modal fade" id="form_modal" aria-hidden="true">
  58. <div class="modal-dialog">
  59. <div class="modal-content">
  60. <form method="POST" action="save_user.php">
  61. <div class="modal-header">
  62. <h3 class="modal-title">Add User</h3>
  63. </div>
  64. <div class="modal-body">
  65. <div class="col-md-2"></div>
  66. <div class="col-md-8">
  67. <div class="form-group">
  68. <label>Firstname</label>
  69. <input type="text" class="form-control" name="firstname" required="required"/>
  70. </div>
  71. <div class="form-group">
  72. <label>Lastname</label>
  73. <input type="text" class="form-control" name="lastname" required="required"/>
  74. </div>
  75. <div class="form-group">
  76. <label>Address</label>
  77. <input type="text" class="form-control" name="address" required="required"/>
  78. </div>
  79. </div>
  80. </div>
  81. <br style="clear:both;"/>
  82. <div class="modal-footer">
  83. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  84. <button name="save" class="btn btn-primary" ><span class="glyphicon glyphicon-save"></span> Save</button>
  85. </div>
  86. </form>
  87. </div>
  88. </div>
  89. </div>
  90. <script src="js/jquery-3.2.1.min.js"></script>
  91. <script src="js/bootstrap.js"></script>
  92. <script src="js/script.js"></script>
  93. </body>
  94. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the MySQLi database server. To do that just copy and write this block of codes inside the text editor, then save it as save_user.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $address = $_POST['address'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  10.  
  11. header("location: index.php");
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will highlight the table data whenever the mouse pointer is hover on it. To make this just copy and write these block of codes below inside the text editor, then save it as script.js inside the js directory.

  1. $(document).ready(function() {
  2. $('.table-row').hover(function() {
  3. $(this).addClass('highlight-row');
  4. }, function() {
  5. $(this).removeClass('highlight-row');
  6. });
  7.  
  8. $("th").hover(function() {
  9. var index = $(this).index();
  10. $(this).css('cursor', 'pointer');
  11. $("th.header, td").filter(":nth-child(" + (index+1) + ")").addClass("highlight-col");
  12. $("th.header").filter(":nth-child(" + (index+1) + ")").attr('class', 'alert-info');
  13. }, function() {
  14. var index = $(this).index();
  15. $("th.header, td").removeClass("highlight-col");
  16. });
  17. });

There you have it we successfully created Simple Table Data Highlighting 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.