How To Filter MySQLi Data Using AngularJS In PHP | CampCodes

How to Filter MySQLi Data Using AngularJS in PHP

July 9, 2020
filter mysqli data using angularjs in php

How to Filter MySQLi Data Using AngularJS in PHP with Source Code

In this tutorial, we will create on How to Filter MySQLi Data Using AngularJS in PHP. This code will filter the MySQLi data when the user enters some keyword in the textbox. The system uses angular directives to retrieve and filter MySQLi data by binding the textbox in the 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 for your system.

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

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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.

filter mysqli data using angularjs in php

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.

    $conn=mysqli_connect("localhost", "root", "", "db_filter");
        die("Error: Failed to connect to database!");

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">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<body ng-controller="myController">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="" >Sourcecodester</a>
    <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">
                    <input type="text" ng-model="firstname" class="form-control"/>
                <div class="form-group">
                    <input type="text" ng-model="lastname" class="form-control"/>
                <div class="form-group">
                    <input type="number" min="0" ng-model="age" class="form-control"/>
                <center><button class="btn btn-primary" ng-click="saveStudent()"><span class="glyphicon glyphicon-save"></span> Save</button></center>
        <div class="col-md-6">
            <div class="form-inline">
                <input type="text" placeholder="Filter" class="form-control" ng-model="keyword"/>
            <br /><br />
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr ng-repeat = "student in students | filter:keyword | orderBy:'lastname'">

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.

    require_once 'conn.php';
    $data = json_decode(file_get_contents("php://input"));
    $firstname = $data->firstname;
    $lastname = $data->lastname;
    $age = $data->age;
    mysqli_query($conn, "INSERT INTO `student` VALUES('', '$firstname', '$lastname', '$age')");


    require_once 'conn.php';
    $data = array();
    $query=mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
        $data[] = $fetch;
    echo json_encode($data);

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){
                        $scope.students =;
                    $scope.getData = function(){
                            $scope.students =;
                    $scope.saveStudent = function(){
                        if($scope.firstname == null || $scope.lastname == null || $scope.age == null){
                            alert("Please complete the required field");
                            $"save.php", {firstname: $scope.firstname, lastname: $scope.lastname, age: $scope.age})
                                $scope.firstname = "";
                                $scope.lastname = "";
                                $scope.age = "";

Leave a Reply

Your email address will not be published. Required fields are marked *