How to make search bar which search value from database example


Like Above search.


Step 1.
Make HTML file as follow.


<html>

  <head>

    <title>Ajax Search Box using PHP and MySQL</title>

    <!-- script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script -->

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>





    <!--script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script -->



    <script src="typeahead.min.js"></script>

    <script>

    $(document).ready(function(){

   $('input.typeahead').typeahead({

        name: 'typeahead',

remote: 'search.php?query=%QUERY',




       // limit : 10

    });

/*var search = new Bloodhound({

    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('sear'),

    queryTokenizer: Bloodhound.tokenizers.whitespace,

    limit: 10,

    remote: {

        url: 'search.php?key=%QUERY',



        filter: function(list) {

            return $.map(list, function(search) {

                return {

                    name: sear

                };

            });


        }

    }

});*/

});

    </script>

    <style type="text/css">

.bs-example{

font-family: sans-serif;

position: relative;

margin: 50px;

}

.typeahead, .tt-query, .tt-hint {

border: 2px solid #CCCCCC;

border-radius: 8px;

font-size: 24px;

height: 30px;

line-height: 30px;

outline: medium none;

padding: 8px 12px;

width: 396px;

}

.typeahead {

background-color: #FFFFFF;

}

.typeahead:focus {

border: 2px solid #0097CF;

}

.tt-query {

box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;

}

.tt-hint {

color: #999999;

}

.tt-dropdown-menu {

background-color: #FFFFFF;

border: 1px solid rgba(0, 0, 0, 0.2);

border-radius: 8px;

box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);

margin-top: 12px;

padding: 8px 0;

width: 422px;

}

.tt-suggestion {

font-size: 24px;

line-height: 24px;

padding: 3px 20px;

}

.tt-suggestion.tt-is-under-cursor {

background-color: #0097CF;

color: #FFFFFF;

}

.tt-suggestion p {

margin: 0;

}

</style>

  </head>

  <body>

    <div class="row">

      

  <div class=".col-md-6">

    <div class="panel panel-default">

    <div class="bs-example">

        <input type="text" name="typeahead" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Type your Query">

    </div>

  </div>

  </div>

  </div>

  </body>

</html>


Step 2- PHP file search from  database.(before that database with user table should exsit.)


<?php

    $key=$_GET['query'];

    $array = array();

   // $con=mysql_connect("localhost","root","");

  //  $db=mysql_select_db("demos",$con);

  

  include_once('../include/config.php');

    $query=mysql_query("select * from user where fname LIKE '{$key}%' or  zkey like '{$key}%' or mobile  like '{$key}%' or email  like '{$key}%' ");

    while($row=mysql_fetch_assoc($query))

    {

      $array[] = $row['fname'].' '.$row['zkey'];

    }

if ($array)


{

    echo json_encode($array);

}

else

{

echo json_encode('Not found');

}

?>

step 3: typeahead.min.js  and bootstrap files are  required to run this


Comments

Popular posts from this blog

What is HTML? Complete Beginner’s Guide (with Examples)

HTML Forms — Input, Label, Button & Validation (Explained Like a Teacher With Examples)

Difference Between HTML and HTML5 (2025 Explained) | Features, Advantages & Comparison Table