AJAX live search does not always contain the full value in the search boxComparison of full text search engine - Lucene, Sphinx, Postgresql, MySQL?Eliminating white space in ajax searchI need Ajax to be able to change a dropdown box selection based on a value from PHP received from SQLmysqli search with while loop output only returns one recordMySQL search in multiple columns using PHP and Ajaxsearch one value in two columns in MySQLPHP AJAX Live SearchSearch a MySQL database for superscript valueDisplay “No matches found” or hide DIV results (AJAX & MySQL)

Does the US Armed Forces refuse to recruit anyone with an IQ less than 83?

How to "Start as close to the end as possible", and why to do so?

Shell Sort, Insertion Sort, Bubble Sort, Selection Sort Algorithms (Python)

Why does it seem the best way to make a living is to invest in real estate?

What's the correct way to determine turn order in this situation?

Postgres sometimes uses inferior index for WHERE a IN (...) ORDER BY b LIMIT N

What is the point of impeaching Trump?

What is the difference between Formal Logic and Proofs?

What is the idiomatic solution in SQL Server for reserving a block of ids for use in a bulk insert?

Is there a pattern for handling conflicting function parameters?

Did Joe Biden "stop a prosecution" into his son in Ukraine? And did he brag about stopping the prosecution?

Does Bank Manager's discretion still exist in Mortgage Lending

Where does the image of a data connector as a sharp metal spike originate from?

Avoiding dust scattering when you drill

Is spot metering just an EV compensation?

Why do personal finance apps focus on outgoings rather than income

Isn't the detector always measuring, and thus always collapsing the state?

Why do Russians sometimes spell "жирный" (fatty) as "жырный"?

Does the 'java' command compile Java programs?

Writing about real people - not giving offence

How to identify whether a publisher is genuine or not?

How can Germany increase investments in Russia while EU economic sanctions against Russia are still in place?

Is the "spacetime" the same thing as the mathematical 4th dimension?

GPLv3 forces us to make code available, but to who?



AJAX live search does not always contain the full value in the search box


Comparison of full text search engine - Lucene, Sphinx, Postgresql, MySQL?Eliminating white space in ajax searchI need Ajax to be able to change a dropdown box selection based on a value from PHP received from SQLmysqli search with while loop output only returns one recordMySQL search in multiple columns using PHP and Ajaxsearch one value in two columns in MySQLPHP AJAX Live SearchSearch a MySQL database for superscript valueDisplay “No matches found” or hide DIV results (AJAX & MySQL)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















I'm setting up a basic AJAX live search for my website but I am encountering an issue where sometimes the whole value of my search box is not being captured properly.



For instance, if I type 'roll' into the search box, sometimes it will return the values for 'ro' instead. This is the most common occurrence as seen here: https://imgur.com/a/u1vLhuz



I say most common occurrence because in the link above, you will see different values captured even though 'roll' is the value of search box for each result. The last image being the desired result. It's as if I type the value in too fast, because I can not reproduce the issue when slowly typing each character.



In desperation I have tried different events, such as keyup, keydown, changed, keypress .on('Keyup') etc. Logging the javascript variable to the console shows the incorrect value, so the sql statement is obviously returning the wrong results (as seen in the album above).



My index.php basically consists of every result before anything is searched:



<ul id="search-results" class="grid cs-style-2 justify-content-center">
$cosmetic->getCard($result);
</ul>


Here is the gist of my ajax.php file:



if (isset($_POST['search'])) 
$search = $_POST['search'];
$sql = "SELECT * FROM Cosmetics WHERE Name LIKE '%$search%' OR Type LIKE '%$search%' OR Rarity LIKE '%$search%' OR CostOrigin LIKE '%$search%' OR ImageName LIKE '%$search%' ORDER BY FIELD(Rarity,'legendary','epic','rare','uncommon','common'), Name";
echo($sql);
$result = $connection->query($sql);
$cosmetic->getCard($result);



and here is the script.js file that is included in the index.php:



$(document).ready(function() 
$("#search").on("keyup", function()
var name = document.getElementById("search").value;
console.log(name);
if (name !== " ")
$.post('/test/ajax.php',search:name, function(data)
$("#search-results").html(data);
);

);
);


In this album: https://imgur.com/a/u1vLhuz I expect the results to be the last image where it actually queries the correct word, instead of capturing other or no values from the search box.










share|improve this question
























  • As you aren't waiting for the whole string to be typed before you start processing it, I think you are having timing problems. I'm wondering what would happen if you simply ran the getElementById and ajax call twice in your function, so that the second call processed the search string as it stood when the first call had finished? Worth a try I think.

    – MandyShaw
    Mar 28 at 23:06











  • Unfortunately seems to still be an issue. I definitely agree though, it does seem to be a timing issue and I tried testing that too, but I don't think it's working. I had var name = $(this).delay(300).val();

    – Fraze
    Mar 29 at 12:48











  • A delay in the script isn't going to help because control is still with the script. You need the script to be releasing control. Have you got the ajax running asynchronously?

    – MandyShaw
    Mar 29 at 13:51











  • Sorry for my ignorance, but isn't it asynchronous by default?

    – Fraze
    Mar 29 at 13:53











  • I don't know, because I don't use JQuery, sorry.

    – MandyShaw
    Mar 29 at 13:55

















0















I'm setting up a basic AJAX live search for my website but I am encountering an issue where sometimes the whole value of my search box is not being captured properly.



For instance, if I type 'roll' into the search box, sometimes it will return the values for 'ro' instead. This is the most common occurrence as seen here: https://imgur.com/a/u1vLhuz



I say most common occurrence because in the link above, you will see different values captured even though 'roll' is the value of search box for each result. The last image being the desired result. It's as if I type the value in too fast, because I can not reproduce the issue when slowly typing each character.



In desperation I have tried different events, such as keyup, keydown, changed, keypress .on('Keyup') etc. Logging the javascript variable to the console shows the incorrect value, so the sql statement is obviously returning the wrong results (as seen in the album above).



My index.php basically consists of every result before anything is searched:



<ul id="search-results" class="grid cs-style-2 justify-content-center">
$cosmetic->getCard($result);
</ul>


Here is the gist of my ajax.php file:



if (isset($_POST['search'])) 
$search = $_POST['search'];
$sql = "SELECT * FROM Cosmetics WHERE Name LIKE '%$search%' OR Type LIKE '%$search%' OR Rarity LIKE '%$search%' OR CostOrigin LIKE '%$search%' OR ImageName LIKE '%$search%' ORDER BY FIELD(Rarity,'legendary','epic','rare','uncommon','common'), Name";
echo($sql);
$result = $connection->query($sql);
$cosmetic->getCard($result);



and here is the script.js file that is included in the index.php:



$(document).ready(function() 
$("#search").on("keyup", function()
var name = document.getElementById("search").value;
console.log(name);
if (name !== " ")
$.post('/test/ajax.php',search:name, function(data)
$("#search-results").html(data);
);

);
);


In this album: https://imgur.com/a/u1vLhuz I expect the results to be the last image where it actually queries the correct word, instead of capturing other or no values from the search box.










share|improve this question
























  • As you aren't waiting for the whole string to be typed before you start processing it, I think you are having timing problems. I'm wondering what would happen if you simply ran the getElementById and ajax call twice in your function, so that the second call processed the search string as it stood when the first call had finished? Worth a try I think.

    – MandyShaw
    Mar 28 at 23:06











  • Unfortunately seems to still be an issue. I definitely agree though, it does seem to be a timing issue and I tried testing that too, but I don't think it's working. I had var name = $(this).delay(300).val();

    – Fraze
    Mar 29 at 12:48











  • A delay in the script isn't going to help because control is still with the script. You need the script to be releasing control. Have you got the ajax running asynchronously?

    – MandyShaw
    Mar 29 at 13:51











  • Sorry for my ignorance, but isn't it asynchronous by default?

    – Fraze
    Mar 29 at 13:53











  • I don't know, because I don't use JQuery, sorry.

    – MandyShaw
    Mar 29 at 13:55













0












0








0








I'm setting up a basic AJAX live search for my website but I am encountering an issue where sometimes the whole value of my search box is not being captured properly.



For instance, if I type 'roll' into the search box, sometimes it will return the values for 'ro' instead. This is the most common occurrence as seen here: https://imgur.com/a/u1vLhuz



I say most common occurrence because in the link above, you will see different values captured even though 'roll' is the value of search box for each result. The last image being the desired result. It's as if I type the value in too fast, because I can not reproduce the issue when slowly typing each character.



In desperation I have tried different events, such as keyup, keydown, changed, keypress .on('Keyup') etc. Logging the javascript variable to the console shows the incorrect value, so the sql statement is obviously returning the wrong results (as seen in the album above).



My index.php basically consists of every result before anything is searched:



<ul id="search-results" class="grid cs-style-2 justify-content-center">
$cosmetic->getCard($result);
</ul>


Here is the gist of my ajax.php file:



if (isset($_POST['search'])) 
$search = $_POST['search'];
$sql = "SELECT * FROM Cosmetics WHERE Name LIKE '%$search%' OR Type LIKE '%$search%' OR Rarity LIKE '%$search%' OR CostOrigin LIKE '%$search%' OR ImageName LIKE '%$search%' ORDER BY FIELD(Rarity,'legendary','epic','rare','uncommon','common'), Name";
echo($sql);
$result = $connection->query($sql);
$cosmetic->getCard($result);



and here is the script.js file that is included in the index.php:



$(document).ready(function() 
$("#search").on("keyup", function()
var name = document.getElementById("search").value;
console.log(name);
if (name !== " ")
$.post('/test/ajax.php',search:name, function(data)
$("#search-results").html(data);
);

);
);


In this album: https://imgur.com/a/u1vLhuz I expect the results to be the last image where it actually queries the correct word, instead of capturing other or no values from the search box.










share|improve this question














I'm setting up a basic AJAX live search for my website but I am encountering an issue where sometimes the whole value of my search box is not being captured properly.



For instance, if I type 'roll' into the search box, sometimes it will return the values for 'ro' instead. This is the most common occurrence as seen here: https://imgur.com/a/u1vLhuz



I say most common occurrence because in the link above, you will see different values captured even though 'roll' is the value of search box for each result. The last image being the desired result. It's as if I type the value in too fast, because I can not reproduce the issue when slowly typing each character.



In desperation I have tried different events, such as keyup, keydown, changed, keypress .on('Keyup') etc. Logging the javascript variable to the console shows the incorrect value, so the sql statement is obviously returning the wrong results (as seen in the album above).



My index.php basically consists of every result before anything is searched:



<ul id="search-results" class="grid cs-style-2 justify-content-center">
$cosmetic->getCard($result);
</ul>


Here is the gist of my ajax.php file:



if (isset($_POST['search'])) 
$search = $_POST['search'];
$sql = "SELECT * FROM Cosmetics WHERE Name LIKE '%$search%' OR Type LIKE '%$search%' OR Rarity LIKE '%$search%' OR CostOrigin LIKE '%$search%' OR ImageName LIKE '%$search%' ORDER BY FIELD(Rarity,'legendary','epic','rare','uncommon','common'), Name";
echo($sql);
$result = $connection->query($sql);
$cosmetic->getCard($result);



and here is the script.js file that is included in the index.php:



$(document).ready(function() 
$("#search").on("keyup", function()
var name = document.getElementById("search").value;
console.log(name);
if (name !== " ")
$.post('/test/ajax.php',search:name, function(data)
$("#search-results").html(data);
);

);
);


In this album: https://imgur.com/a/u1vLhuz I expect the results to be the last image where it actually queries the correct word, instead of capturing other or no values from the search box.







php mysql ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 21:02









FrazeFraze

1




1















  • As you aren't waiting for the whole string to be typed before you start processing it, I think you are having timing problems. I'm wondering what would happen if you simply ran the getElementById and ajax call twice in your function, so that the second call processed the search string as it stood when the first call had finished? Worth a try I think.

    – MandyShaw
    Mar 28 at 23:06











  • Unfortunately seems to still be an issue. I definitely agree though, it does seem to be a timing issue and I tried testing that too, but I don't think it's working. I had var name = $(this).delay(300).val();

    – Fraze
    Mar 29 at 12:48











  • A delay in the script isn't going to help because control is still with the script. You need the script to be releasing control. Have you got the ajax running asynchronously?

    – MandyShaw
    Mar 29 at 13:51











  • Sorry for my ignorance, but isn't it asynchronous by default?

    – Fraze
    Mar 29 at 13:53











  • I don't know, because I don't use JQuery, sorry.

    – MandyShaw
    Mar 29 at 13:55

















  • As you aren't waiting for the whole string to be typed before you start processing it, I think you are having timing problems. I'm wondering what would happen if you simply ran the getElementById and ajax call twice in your function, so that the second call processed the search string as it stood when the first call had finished? Worth a try I think.

    – MandyShaw
    Mar 28 at 23:06











  • Unfortunately seems to still be an issue. I definitely agree though, it does seem to be a timing issue and I tried testing that too, but I don't think it's working. I had var name = $(this).delay(300).val();

    – Fraze
    Mar 29 at 12:48











  • A delay in the script isn't going to help because control is still with the script. You need the script to be releasing control. Have you got the ajax running asynchronously?

    – MandyShaw
    Mar 29 at 13:51











  • Sorry for my ignorance, but isn't it asynchronous by default?

    – Fraze
    Mar 29 at 13:53











  • I don't know, because I don't use JQuery, sorry.

    – MandyShaw
    Mar 29 at 13:55
















As you aren't waiting for the whole string to be typed before you start processing it, I think you are having timing problems. I'm wondering what would happen if you simply ran the getElementById and ajax call twice in your function, so that the second call processed the search string as it stood when the first call had finished? Worth a try I think.

– MandyShaw
Mar 28 at 23:06





As you aren't waiting for the whole string to be typed before you start processing it, I think you are having timing problems. I'm wondering what would happen if you simply ran the getElementById and ajax call twice in your function, so that the second call processed the search string as it stood when the first call had finished? Worth a try I think.

– MandyShaw
Mar 28 at 23:06













Unfortunately seems to still be an issue. I definitely agree though, it does seem to be a timing issue and I tried testing that too, but I don't think it's working. I had var name = $(this).delay(300).val();

– Fraze
Mar 29 at 12:48





Unfortunately seems to still be an issue. I definitely agree though, it does seem to be a timing issue and I tried testing that too, but I don't think it's working. I had var name = $(this).delay(300).val();

– Fraze
Mar 29 at 12:48













A delay in the script isn't going to help because control is still with the script. You need the script to be releasing control. Have you got the ajax running asynchronously?

– MandyShaw
Mar 29 at 13:51





A delay in the script isn't going to help because control is still with the script. You need the script to be releasing control. Have you got the ajax running asynchronously?

– MandyShaw
Mar 29 at 13:51













Sorry for my ignorance, but isn't it asynchronous by default?

– Fraze
Mar 29 at 13:53





Sorry for my ignorance, but isn't it asynchronous by default?

– Fraze
Mar 29 at 13:53













I don't know, because I don't use JQuery, sorry.

– MandyShaw
Mar 29 at 13:55





I don't know, because I don't use JQuery, sorry.

– MandyShaw
Mar 29 at 13:55












1 Answer
1






active

oldest

votes


















0
















This query should output them in the correct order.
First items: Exact match to search term
Next items: Items that start with search term
Third items: All items that include search term



SELECT * 
FROM Cosmetics
WHERE Name LIKE '%$search%'
ORDER BY (CASE WHEN name = '$search' THEN 1 WHEN name LIKE '$search%' THEN 2 ELSE 3 END);





share|improve this answer



























  • Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

    – Fraze
    Mar 29 at 12:51












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406799%2fajax-live-search-does-not-always-contain-the-full-value-in-the-search-box%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0
















This query should output them in the correct order.
First items: Exact match to search term
Next items: Items that start with search term
Third items: All items that include search term



SELECT * 
FROM Cosmetics
WHERE Name LIKE '%$search%'
ORDER BY (CASE WHEN name = '$search' THEN 1 WHEN name LIKE '$search%' THEN 2 ELSE 3 END);





share|improve this answer



























  • Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

    – Fraze
    Mar 29 at 12:51















0
















This query should output them in the correct order.
First items: Exact match to search term
Next items: Items that start with search term
Third items: All items that include search term



SELECT * 
FROM Cosmetics
WHERE Name LIKE '%$search%'
ORDER BY (CASE WHEN name = '$search' THEN 1 WHEN name LIKE '$search%' THEN 2 ELSE 3 END);





share|improve this answer



























  • Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

    – Fraze
    Mar 29 at 12:51













0














0










0









This query should output them in the correct order.
First items: Exact match to search term
Next items: Items that start with search term
Third items: All items that include search term



SELECT * 
FROM Cosmetics
WHERE Name LIKE '%$search%'
ORDER BY (CASE WHEN name = '$search' THEN 1 WHEN name LIKE '$search%' THEN 2 ELSE 3 END);





share|improve this answer















This query should output them in the correct order.
First items: Exact match to search term
Next items: Items that start with search term
Third items: All items that include search term



SELECT * 
FROM Cosmetics
WHERE Name LIKE '%$search%'
ORDER BY (CASE WHEN name = '$search' THEN 1 WHEN name LIKE '$search%' THEN 2 ELSE 3 END);






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 21:29

























answered Mar 28 at 21:23









RalphRalph

2272 silver badges10 bronze badges




2272 silver badges10 bronze badges















  • Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

    – Fraze
    Mar 29 at 12:51

















  • Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

    – Fraze
    Mar 29 at 12:51
















Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

– Fraze
Mar 29 at 12:51





Hi Ralph. Thank you, but the issue isn't the order they're in. It's the value being captured from the search box isn't always the whole value.

– Fraze
Mar 29 at 12:51




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406799%2fajax-live-search-does-not-always-contain-the-full-value-in-the-search-box%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript