Searching for address and pin code on a Database with Autocomplete.
User will enter matching keys for searching a location. Using Autocomplete we can show the available names which the user can select. On selection of the location, full details like division, Region, Circle, Taluk, district, state with Pin Code will be displayed.
For this a single table with all required data is maintained. Source of Autocomplete is name column storing the location.
We kept one textbox to receive the user entered text.
To get the suggestions for autocomplete we used source option. We are posting the user entered chars to backend PHP script search-keyword-backend.php. We getback the return value in lable and value pairs, this will be displayed to user as suggested list for its selection.
We also used another image as button to reset the input box. We used load() to post the unique id of the record to backend script and get back the all the details of the record to display to user.
Here is the complete front end code to to post the data to backend script and get the output to display.
We were posting the user entered keyword to backend script and getting the matching records as suggestions for user to select. Now we will send one more parameter, the state name along with the user entered chars to restrict the return suggestion limited to a particular state only.
One dropdown list showing the state names is added for the user to select the name of the state to restrict the suggestions to that state only.
There are two pages with PHP code to manage the database. The first one indexck.php manages the suggestions for autocomplete so users can select one of the options. The second one indexck-dtl.php receives the id of the record based on the user selection of option and then returns the full details of that record for display.
indexck.php
This page receives the user entered chars and the name of the state selected by dropdown listbox. ( in case of index-state.php page ).
These two parameters are then used to create the sql query by using WHERE condition check using LIKE query to match user entered chars.
By entering DA [space] BA we can get records matching to both DA or BA.
While displaying the options we have combined officename and statename by using CONCAT function.
Using the query through fetch_array() function, we filled an array of options by value and label pairs. The same array we use to crate json output to post back to our main scripts.
<?Php
$o_name=$_REQUEST['term']; // receive the keyword or chars
$statename=$_GET['statename']; // receive the state name
require "config.php";// connection to database
$search = array('&', ' ');
$replace = array('', '');
if(!ctype_alpha(str_replace($search,$replace,$o_name))){
echo "Data Error ";
exit;
}
if(strlen($statename) >0 ){
if(!ctype_alpha(str_replace($search,$replace,$statename))){
echo "Data Error ";
exit;
}
$q2= " AND statename='$statename' ";
}else {$q2='';}
$kt=explode(" ",$o_name);//Breaking the string to array of words
// Now let us generate the sql
while(list($key,$val)=each($kt)){
if($val<>" " and strlen($val) > 0){$q .= " officename like '%$val%' or ";}
}// end of while
$q=substr($q,0,(strlen($q)-3));
$my_array=array();
$q1="select CONCAT(officename,',',statename) as label, id as value from pincode_india where $q $q2 limit 0,30";
$result_set = $connection->query($q1);
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
$my_array[]=array("value"=>$row['value'],"label"=>$row['label']);
}
echo json_encode($my_array);
?>
indexck-dtl.php
This file receives the ID of the record ( location ) as selected by user from the suggestion of autocomplete. Using this ID all details of the record is collected and posted back to main page.