jquery autocomplete from database code example
Example 1: jquery autocomplete database
<?php
require_once('connection.php');
function get_city($conn , $term){
$query = "SELECT * FROM city WHERE city_name LIKE '%".$term."%' ORDER BY city_name ASC";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
return $data;
}
if (isset($_GET['term'])) {
$getCity = get_city($conn, $_GET['term']);
$cityList = array();
foreach($getCity as $city){
$cityList[] = $city['city_name'];
}
echo json_encode($cityList);
}
?>
Example 2: jquery autocomplete database
<?php
require "database/database.php";
$data = mysql_query("SELECT * FROM countries");
$result = array();
while($row = mysql_fetch_assoc($data)) {
$result[] = array(
'label' = $row['countryname'],
'value' = $row['countryname']
);
}
echo json_encode($result);
flush();
}
?>