PHP - Simple Filter MySQLi Data Using AngularJS
Submitted by razormist on Wednesday, June 12, 2019 - 10:44.
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.
data.php
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!!
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 bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, this is the link for the AngularJS https://angularjs.org/.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.- <?php
- if(!$conn){
- }
- ?>
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.- <!DOCTYPE html>
- <html lang="en" ng-app="myModule">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body ng-controller="myController">
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com" >Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Simple Filter MySQLi Data Using AngularJS</h3>
- <hr style="border-top:1px dotted #ccc;">
- <div class="col-md-6">
- <form method="POST">
- <div class="form-group">
- <label>Firstname</label>
- <input type="text" ng-model="firstname" class="form-control"/>
- </div>
- <div class="form-group">
- <label>Lastname</label>
- <input type="text" ng-model="lastname" class="form-control"/>
- </div>
- <div class="form-group">
- <label>Age</label>
- </div>
- <center><button class="btn btn-primary" ng-click="saveStudent()"><span class="glyphicon glyphicon-save"></span> Save</button></center>
- </form>
- </div>
- <div class="col-md-6">
- <div class="form-inline">
- <input type="text" placeholder="Filter" class="form-control" ng-model="keyword"/>
- </div>
- <br /><br />
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Age</th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat = "student in students | filter:keyword | orderBy:'lastname'">
- <td>{{student.firstname}}</td>
- <td>{{student.lastname}}</td>
- <td>{{student.age}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- <script src="js/angular.js"></script>
- <script src="js/script.js"></script>
- </body>
- </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. save.php- <?php
- require_once 'conn.php';
- $firstname = $data->firstname;
- $lastname = $data->lastname;
- $age = $data->age;
- ?>
- <?php
- require_once 'conn.php';
- $data[] = $fetch;
- }
- ?>
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- var app = angular.module("myModule", [])
- .controller("myController", function($scope, $http){
- $http.get('data.php').then(function(response){
- $scope.students = response.data;
- });
- $scope.getData = function(){
- $http.get('data.php').then(function(response){
- $scope.students = response.data;
- });
- }
- $scope.saveStudent = function(){
- if($scope.firstname == null || $scope.lastname == null || $scope.age == null){
- alert("Please complete the required field");
- }else{
- $http.post("save.php", {firstname: $scope.firstname, lastname: $scope.lastname, age: $scope.age})
- .then(function(){
- $scope.firstname = "";
- $scope.lastname = "";
- $scope.age = "";
- $scope.getData();
- });
- }
- }
- });
Add new comment
- 103 views