I'm trying to make a search bar which sends a request for proposals from the entered letter. jQuery is sending the same number of requests as the number of the entered letters.
If I write "sa" jQuery is sending 2 requests. If I write "Sa" jQuery is sending 3 Requests (the big letter is counted as 2 letters since I'm pressing shift".
The code should work like the following instructions: The user writes the name of what he wants to found and the code search in the database and shows all possible search words. EDITS: the script should send 2 Request: the first one to get all results which have "sa". after selecting a result. it should send only one request to the database to get the rest of the information. what my script is doing: he is sending the firs request as he should do. the second request is getting send as same time as the number of the letters. even when only one request is enough
here is also a link to a youtube video which I record to explain the problem
Here is my jQuery code:
$('.sucherInput').on('keyup', function () {
// ** .sucherInput is the class of the input field
var inputValue = $('.sucherInput').val();
var result = $(".sucherres");
var resultList = $(".sucherres ul");
if (inputValue.length) {
console.log(inputValue);
$.post("action/php/artikelSucher.php", {
name: inputValue
}).done(function(data){
// Display the returned data in browser
//console.log(data);
resultList.html(data);
result.show();
}); //.done
} else{
console.log("empty");
resultList.empty();
result.hide();
}
$(document).on("click", ".sucherres ul li p", function(){
//set the search bar value as same as the clicked <p> tag
$('.sucherInput').val($(this).text());
//clear the Proposals list
$(resultList).empty();
$(result).hide();
//renew the value of the search bar
//since im taking the value which have to be searched in the database from the searchbar
var sucherLastIndex = $('.sucherInput').val();
console.log("Getting data from database: " + sucherLastIndex);
//load the data into the HTML file
$("#updateDiv #info").load("action/php/index-preis-liste.php", {
name: sucherLastIndex
});
});
});
Here is the Html Code:
<div class="sucher">
<ul style="width: 100%; margin-bottom: 0;">
<li style="width: 33%;">
<input type="text" class="sucherInput" placeholder="Geben Sie einen Suchbegriff ein">
</li>
</ul>
<div class="sucherres">
<ul>
<!-- ## HERE COMES THE Proposals ## -->
</ul>
</div>
<div id="info">
</div>