Monday, March 27, 2017

Simple Live Data Search of Mysql table using Jquery Ajax And PHP Mysqli

This is a Simple  Live Data Search of Mysql table using Jquery Ajax  And PHP Mysqli.

  • First Create A Database called "10am" in phpmyadmin.
  • After Creating Database in phpmyadmin create table called 'lalita' .
  • Then Add Column's on lalita as id and set as autoincrement ,Name,Address,City,Country and set as varchar.
  • And last of database section insert some values on table columns

Now Create php file index.php

 Copy this php scripts and paste it on index.php file .          
  1. <html>  
  2.       <head>  
  3.            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4.            <title>bramento online study</title>  
  5.            <script src="js/jquery-1.10.2.min.js"></script>  
  6.            <script src="js/bootstrap.min.js"></script>  
  7.            <link href="css/bootstrap.min.css" rel="stylesheet" />  
  8.       </head>  
  9.       <body>  
  10.            <div class="container">  
  11.                 <br />  
  12.                 <h2 align="center"> Simple  Live Data Search of Mysql table using Jquery Ajax  And PHP Mysqli.</h2><br />  
  13.                 <div class="form-group">  
  14.                      <div class="input-group">  
  15.                           <span class="input-group-addon">Search</span>  
  16.                           <input type="text" name="search" id="search_text" placeholder="Write Something To Search" class="form-control" />  
  17.                      </div>  
  18.                 </div>  
  19.                 <br />  
  20.                 <div id="result"></div> 
  21. //javascripts to call ajax
  22.                  <script>  
  23.  $(document).ready(function(){  
  24.       $('#search_text').keyup(function(){  
  25.            var txt = $(this).val();  
  26.            if(txt != '')  
  27.            {  
  28.                 $.ajax({  
  29.                      url:"fetch.php",  
  30.                      method:"post",  
  31.                      data:{search:txt},  
  32.                      dataType:"text",  
  33.                      success:function(data)  
  34.                      {  
  35.                           $('#result').html(data);  
  36.                      }  
  37.                 });  
  38.            }  
  39.            else  
  40.            {  
  41.                 $('#result').html('');                 
  42.            }  
  43.       });  
  44.  });  
  45.  </script>  
  46.  </div>  
  47.       </body>  
  48.  </html>  

Now Create another php file called fetch.php

Copy and paste this php scripts on fetch.php file.

  1. <?php  
  2. // Database Connection 
  3.  $connect = mysqli_connect("localhost", "root", "", "multi");  
  4.  $output = '';  
  5. // Query for Select And Search 
  6.  $sql = "SELECT * FROM lalita WHERE Name LIKE '%".$_POST["search"]."%'";  
  7.  $result = mysqli_query($connect, $sql);  
  8.  if(mysqli_num_rows($result) > 0)  
  9.  {  
  10.       $output .= '<h3 align="center">Search Found</h3>';  
  11.       $output .= '<div class="table-responsive">  
  12.                           <table class="table table bordered">  
  13.                                <tr>  
  14.                                     <th>Name</th>  
  15.                                     <th>Address</th>  
  16.                                     <th>City</th>    
  17.                                     <th>Country</th>  
  18.                                </tr>';  
  19.       while($row = mysqli_fetch_array($result))  
  20.       {  
  21.            $output .= '  
  22.                 <tr>  
  23.                      <td>'.$row["Name"].'</td>  
  24.                      <td>'.$row["Address"].'</td>  
  25.                      <td>'.$row["City"].'</td>            
  26.                      <td>'.$row["Country"].'</td>  
  27.                 </tr>  
  28.            ';  
  29.       }  
  30.       echo $output;  
  31.  }  
  32.  else  
  33.  {  
  34.       echo 'Data Does Not Found Here';  
  35.  }  
  36.  ?>  

No comments:

Post a Comment