javascript - How to not allow values that are not in the database in an Autocomplete form with PHP & JS
I have an autocomplete function with a form where the name of the city is type into. All possible values are in a database and are pull out of the database and store them in the variable $list
Then I have my form with the input and the button
<form name="form1" method="post" action="searchresults.php" >
<input id="hero-demo" autofocus type="search" name="search" placeholder="City" >
<input type="submit" name="submit" value="Search">
</form>
and the autocomplete function:
<script>
$(function(){
$('#hero-demo').autoComplete({
minChars: 1,
source: function(term, suggest){
term = term.toLowerCase();
var choices = [<?= $list ?>];
var suggestions = [];
for (i=0;i<choices.length;i++)
if (~choices[i].toLowerCase().indexOf(term)) suggestions.push(choices[i]);
suggest(suggestions);
}
});
});
</script>
How can I do, so if for instance the user introduce the value "London" and that value is not in my database ($list) then the Search button doesn't work.
Thank you very much
Answer
Solution:
You could add on
submit
event in your js code then do what you want before submiting the form and you should remove inline-event from your HTML code in this case.HTML :
JS :
Hope this helps.