Does anyone know why my AutoComplete is not working when the first character I type is 0 (zero)? For debugging purposes I setup my AC to just tell me a row was found or not, and it seems that for any character I type as the first character it tells me so, except 0. I have to type in a second character after 0 for it to kick in and start working. It’s as if the minLength attribute is 2 when the first char is 0. Has anyone run into or heard of this and know how to fix it? Here is my code:
//AutoComplete code in question
$(function() {
var itemcode_ac = {
source: "/webservices/whs_bincodeAC.php",
select: function(event, ui) {
$('#txtBin').val(ui.item.value);
getWhsInfo();
},
minLength: 1
}
$('#txtBin').autocomplete(itemcode_ac);
});
whs_bincodeAC.php:
<?php
if(isset($_GET["term"]) && !empty($_GET["term"])) {
include_once $_SERVER['DOCUMENT_ROOT'].'/path/to/dbConnect.php';
$term = mysql_real_escape_string(trim($_GET["term"]));
//wildcard appended here for parameterized query (MySqli)
$term .= "%";
$query = "SELECT DISTINCT BinCode, ItemCode, ItemName, WhsCode, DataAsOfDate FROM whse_tbl
WHERE BinCode LIKE '$term' or ItemCode LIKE '$term' ORDER BY BinCode LIMIT 0, 10";
$res = mysql_query($query);
//This is the debug code I described above
/*if($row = mysql_fetch_assoc($res))
echo json_encode(array(array('value' => "is row")));
else
echo json_encode(array(array('value' => "no row")));
return;*/
$matches = array();
while($row = mysql_fetch_assoc($res))
{
$matches[] = array('value' => $row["BinCode"], 'label' => $row["BinCode"].' - '.$row["ItemCode"],
'name' => $row["ItemName"], 'whscode' => $row["WhsCode"], 'asOfDate' => $row["DataAsOfDate"]);
}
echo json_encode($matches);
}
?>
Note: My boss is having me use MySql and not MySqli extension for now.
Probably you’re doing something like the following.
if(empty($_GET["bin"]))orif(!$_GET["bin"])to check it’s value.But in this cases if
binis0, the first case results intrueand the second infalse.So use
isset($_GET["bin"])instead.