Monday, November 7, 2016

Autocomplete Using Jquery & PHP

///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
function abc(){
var minlength=2;

    var chk=document.getElementById("country_name").value;
   if(chk.length>=minlength){
   $.ajax({
 type:"POST",
 url:"fetchresult.php",
 data:{"hints":chk},
 success:function(data){
 $("#hints").html(data)
 }

});
   }

}
</script>


<form action="#" method="post">
<table>
<tr>
  <td>
    <input type="text" id="country_name" name="country_name" onkeyup="javascript:abc()" />
<div><ul id="hints"></ul></div>
  </td>
</tr>

<tr>
  <td>
    <input type="submit" id="sub" name="sub" value="Enter" />
  </td>
</tr>
</table>
</form>

<?php
  if(isset($_REQUEST['sub'])){
    echo "comming here";
  }
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// fetchresult.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php
include("db.php");
$obj=new DB;
$hints="in";
//$obj->hc($sql);tbl_country where countryname LIKE
$hints=$_REQUEST['hints'];
$sql="select * from tbl_country where countryname LIKE  '%$hints%'";
 //echo $sql="select * from tbl_country where countryname LIKE '%$hints%' order by id asc limit 1,10";
$results=$obj->getTable($sql);
  foreach($results as $res){
     echo '<li>'.$res['countryname'].'</li>';
  }



?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End fetchresult.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// db.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php
class DB{
   private $dbhost="localhost";
   private $dbuser="root";
   private $dbpass="";
   private $dbname="countrysql";






public function hc($sql,$ch){
  $result=mysql_query($sql,$this->getCon());
while($res=mysql_fetch_assoc($result)){
if($res['name']==$ch){
 $resarr[]=array('name'=>$res['name'],'y'=>$res['amount'],'sliced'=>"true",'selected'=>"true");
 }

else{
 $resarr[]=array('name'=>$res['name'],'y'=>$res['amount']);
}
}
//$res=json_encode($resarr, JSON_NUMERIC_CHECK);
return $resarr;
}





 public function getTable($sql){
  $result=mysql_query($sql,$this->getCon());
while($row=mysql_fetch_array($result)){
 $resarr[]=$row;
}
return $resarr;
 }



  public function getCon(){
  $con=mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
  mysql_select_db($this->dbname,$con)or
  die(mysql_error("can't connect db"));
  return $con;
 }


}//class end

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End db.php
///////////////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment