Auto Search Using JQuery In PHP | CampCodes

Auto Search Using jQuery in PHP

June 2, 2020
Auto Search Using jQuery in PHP

In this tutorial, we will create an Auto-Search using PHP. This code will auto search specific data in the MySQLi server when the user entered a keyword. The code use jQuery ajax to automatically search a table row data from the MySQLi server without a refreshing web page and modify the HTML table to display the result. This is a user-friendly kind of program feel free to change it and use it as your own.

We will be using jQuery, a JavaScript framework that designs to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each property 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 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

Creating Database

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

autosearch 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 = new mysqli('localhost', 'root', '', 'db_auto');
        die("Error: Cam't 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 your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
            <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
            <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <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 - Auto Search Using jQuery</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form action="save.php" method="POST">
                <div class="form-group">
                    <input type="text" class="form-control" name="firstname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="lastname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="address" required="required"/>
                <center><button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Add Member</button></center>
        <div class="col-md-8">
            <div class="form-group">
                <input class="form-control" type="text" id="search" placeholder="Search here..."/>
            <div id="result"></div>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/script.js"></script>

Creating the PHP Query

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

    require_once 'conn.php';
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $address = $_POST['address'];
        $conn->query("INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_errno());
        header('location: index.php');

Creating the Main Function

This code contains the main function of the application. This code will auto search a specific data in the database when the keyword is entered. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.

    $('#search').on('keyup', function(){
        if($("#search").val() == ""){
            var search = $("#search").val();
                url: 'auto_search.php',
                type: 'POST',
                data: {search: search},
                dataType: 'text',
                success: function(data){

There you have it we successfully created Auto Search using PHP. 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!

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

Leave a Reply

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