Reputation: 621
I am trying to get autocomplete suggestions for the contents of textbox and also based on value from a 'Select' element. The suggestion from mysql db is based on both textbox contents and 'Select' element value.
Below autocomplete for text field which has 'criteriafield' as id only posts 'Vendor' as value for 'f' parameter to ac.php even the 'Select' element's value is not 'Vendor' so I get suggestions only for 'Vendor' value.
Is this wrong way or possible to get latest content of element in jQuery autocomplete?
htmlfile.htm
<html><head><title>CRM</title>
$(function() {
$( "#filtercriteria" ).autocomplete({
source: "ac.php?n=crmsearch&f="+$('#criteriafield').val(),
minLength:3
}); }
</head>
<body>
<select id='filtercriteria'>
<option>Vendor</option>
<option>Contact</option>
</select>
<input type='text' id='criteriafield'>
</body>
Below is my mysql query from php file to make my question more clear.
ac.php:
$connection=mysqli_connect("10.10.10.124","root","root","crmdb");
if ($_GET["n"]=="crmsearch"){
$field=$_GET["f"];
$tbl="crm";
}
$q=$_GET["term"];
$sql="SELECT DISTINCT `".$field."` FROM $tbl WHERE `".$field."` LIKE '%$q%' LIMIT 50";
$result = mysqli_query($connection,$sql);
$json=array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
array_push($json, $row[$field]);
}
echo json_encode($json);
mysqli_close($connection);
JSFiddle: https://jsfiddle.net/dhay/gszw2rn7/2/
Edit:
For now I have added another autocomplete function with
$( "#criteriafield" ).change(function() {$( "#filtercriteria" ).autocomplete({ source: "ac.php?n=crmsearch&f="+$('#criteriafield').val(), minLength:3 });});
It works for now as expected. I wouldn't say this is my expected result but a workaround. So I welcome suggestions if there is another proper way. Thank you.
Upvotes: 0
Views: 91