Ajax Live Data Search using Jquery PHP MySql




INDEX.PHP


<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>WIKIPHP Tutorial</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
 </head>
 <body>
  <div class="container">
   <br />
   <h2 align="center">Ajax Live Data Search using Jquery PHP MySql</h2><br />
   <div class="form-group">
    <div class="input-group">
     <span class="input-group-addon">Search</span>
     <input type="text" name="search_text" id="search_text" placeholder="Search by Customer Details" class="form-control" />
    </div>
   </div>
   <br />
   <div id="result"></div>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){

 load_data();

 function load_data(query)
 {
  $.ajax({
   url:"fetch.php",
   method:"POST",
   data:{query:query},
   success:function(data)
   {
    $('#result').html(data);
   }
  });
 }
 $('#search_text').keyup(function(){
  var search = $(this).val();
  if(search != '')
  {
   load_data(search);
  }
  else
  {
   load_data();
  }
 });
});
</script>



FETCH.PHP



//fetch.php
$connect = mysqli_connect("localhost", "root", "", "ajax");
$output = '';
if(isset($_POST["query"]))
{
 $search = mysqli_real_escape_string($connect, $_POST["query"]);
 $query = "
  SELECT * FROM tbl_customer 
  WHERE CustomerName LIKE '%".$search."%'
  OR Address LIKE '%".$search."%' 
  OR City LIKE '%".$search."%' 
  OR PostalCode LIKE '%".$search."%' 
  OR Country LIKE '%".$search."%'
 ";
}
else
{
 $query = "
  SELECT * FROM tbl_customer ORDER BY CustomerID
 ";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) &gt; 0)
{
 $output .= '
 
   

   
     
Customer Name

     
Address

     
City

     
Postal Code

     
Country

   
 ';
 while($row = mysqli_fetch_array($result))
 {
  $output .= '
   
   
'.$row["CustomerName"].'

   
'.$row["Address"].'

   
'.$row["City"].'

   
'.$row["PostalCode"].'

   
'.$row["Country"].'

   
  ';
 }
 echo $output;
}
else
{
 echo 'Data Not Found';
}


?&gt;

Ajax Live Data Search using Jquery PHP MySql Ajax Live Data Search using Jquery PHP MySql Reviewed by Viththiyakaran on 9:03 PM Rating: 5

No comments:

Powered by Blogger.