I am using AJAX to execute a mysql LIKE query and display the result set data using html.
It seems that the results returned from the query always use whatever is stored in the searchword variable minus the last character and I do not know why.
MySQL: ("SELECT * FROM products WHERE prod_name LIKE '".$searchword."%'")
var_dump when searchword var contains ‘bab’ = string(49) "SELECT * FROM products WHERE prod_name LIKE 'ba%'"
JQuery/ AJAX:
function search(searchword) {
$('.smart-suggestions').load('invoice-get-data.php?searchword=' + searchword);
}
Markup
<input onKeyPress="search(this.value)" type="text" class="prod-name-input"/>
The problem is that onkeypress fires before the value of the text field gets updated. This is why you’re seeing the one character delay. To fix this, you can use onkeyup instead.