Reason: Want to speedup autocoplete result
What i have: I am having two Input boxes. One is Category and another one is Variety. First im selecting Category by,
$("#category").autocomplete({
autoFocus: true,
source: function(request, response) {
$.getJSON("./php/autocompletenontex_getcategory.php", { category: request.term }, response);
},
minLength: 0,
select: function( event, ui ) {
$('#variety').focus();
}
}).focus(function() {
$(this).autocomplete("search", "");
});
The above code shows autocomplete result before start typing.
My PHP Code is: //after including DB connection
$category = $_REQUEST["category"];
$sql = "SELECT distinct(category) FROM master_nontextile WHERE category LIKE '%".$category."%'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$results[] = array('label' => $row['category'], 'value' => $row['category']);
}
echo json_encode($results);
The next Input Field is Variety - variety autocomplete result is based on category's selection. The Code is below
$("#variety").autocomplete({
autoFocus: true,
source: function(request, response) {
$.getJSON("./php/autocompletenontex_getvariety.php", { variety: request.term, category: $("#category").val() }, response);
},
minLength: 0,
select: function( event, ui ) {
$('#variety').val(ui.item.variety);
$('#price').val(ui.item.price);
$('#quantity').focus();
}
}).focus(function() {
$(this).autocomplete("search", "");
});
Corresponding PHP is:
//after including DB connection
$stmt = $pdo->prepare("SELECT variety, price FROM master_nontextile WHERE category = '$category' AND ( variety LIKE '%".$variety."%' OR price ='$variety' ) ORDER BY variety ASC"); $stmt->execute();
while($row = $stmt->fetch()){
$results[] = array('label' => $row['variety'].' - '.$row['price'], 'value' => $row['variety'], 'price' => $row['price']);
}
echo json_encode($results);
Problem: We have a billing application. In variety field they can type a variety name or price. While typing the autocomplete result shows the result after 1 second (Client is feeling this is tooooo late). So i need it to populate fast on typing price or variety name. i think it is taking time to make connection then retireving is taking time.
I came to know we can load datas on page load by using ajax request link here This solution is ok for one independent autocomplete field. But im having two input field, the second one's result is depends on first one. so how can i achieve it without going to DB?