PHP - Simple Filter MySQLi Data Using AngularJS

Submitted by: 
Visitors have accessed this post 204 times.

In this tutorial we will create a Simple Filter MySQLi Data Using AngularJS. This code will filter the MySQLi data when the user enter some keyword in the textboxt. The code use angular directives to retrieve and filter MySQLi data by binding the textbox in ng-model and adding filter function in the ng-repeat with the same value as the model. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

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

Lastly, this is the link for the AngularJS

Creating Database

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

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_filter");
  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 you text editor, then save it index.php.

  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  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 ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href=""" rel="nofollow">"</a> >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 Filter MySQLi Data Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-6">
  18. <form method="POST">
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input type="text" ng-model="firstname" class="form-control"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input type="text" ng-model="lastname" class="form-control"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Age</label>
  29. <input type="number" min="0" ng-model="age" class="form-control"/>
  30. </div>
  31. <center><button class="btn btn-primary" ng-click="saveStudent()"><span class="glyphicon glyphicon-save"></span> Save</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-6">
  35. <div class="form-inline">
  36. <input type="text" placeholder="Filter" class="form-control" ng-model="keyword"/>
  37. </div>
  38. <br /><br />
  39. <table class="table table-bordered">
  40. <thead class="alert-info">
  41. <tr>
  42. <th>Firstname</th>
  43. <th>Lastname</th>
  44. <th>Age</th>
  45. </tr>
  46. </thead>
  47. <tbody>
  48. <tr ng-repeat = "student in students | filter:keyword | orderBy:'lastname'">
  49. <td>{{student.firstname}}</td>
  50. <td>{{student.lastname}}</td>
  51. <td>{{student.age}}</td>
  52. </tr>
  53. </tbody>
  54. </table>
  55. </div>
  56. </div>
  57. <script src="js/angular.js"></script>
  58. <script src="js/script.js"></script>
  59. </body>
  60. </html>

Creating PHP Query

This code contains the php query of the application. This code will store and retrieve the data inputs that send to the database server. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.

  1. <?php
  2. require_once 'conn.php';
  3. $data = json_decode(file_get_contents("php://input"));
  5. $firstname = $data->firstname;
  6. $lastname = $data->lastname;
  7. $age = $data->age;
  9. mysqli_query($conn, "INSERT INTO `student` VALUES('', '$firstname', '$lastname', '$age')");
  12. ?>


  1. <?php
  2. require_once 'conn.php';
  4. $data = array();
  6. $query=mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  9. while($fetch=mysqli_fetch_array($query)){
  10. $data[] = $fetch;
  11. }
  13. echo json_encode($data);
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will save and fetch the data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it inside the js directory as script.js

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http){
  3. $http.get('data.php').then(function(response){
  4. $scope.students =;
  5. });
  8. $scope.getData = function(){
  9. $http.get('data.php').then(function(response){
  10. $scope.students =;
  11. });
  12. }
  14. $scope.saveStudent = function(){
  15. if($scope.firstname == null || $scope.lastname == null || $scope.age == null){
  16. alert("Please complete the required field");
  17. }else{
  18. $"save.php", {firstname: $scope.firstname, lastname: $scope.lastname, age: $scope.age})
  19. .then(function(){
  20. $scope.firstname = "";
  21. $scope.lastname = "";
  22. $scope.age = "";
  23. $scope.getData();
  24. });
  25. }
  27. }
  28. });

There you have it we successfully created a Simple Filter MySQLi Data using AngularJS. 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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.