Jump to content

Save user search words in live search


emptry

Recommended Posts

Hello Freaks,

 

I working on a little snippet, a live search with MySQL.

 

Now i think it could be nice to store/save which searchword the user, did the search on.

Example:

 

User search on

My new book

 

Then i want to store that to my databse.

The problem is with my script right now, where i trig the ajax on keyup. Then it will store.

M
My
My 
My N
My Ne......
 

and so on, how can i come around this and only store the hole line ..?

Link to comment
Share on other sites

haha my friend. i havn't made the script, because i dont know how i should make it :)

 

I can make it so it will store every single letter.. but that's not what i want :)

But here is my code for the live search  :)
 

$(function() {
    $("#searchword").keyup(function(){
        var text = $(this).val();
        if (text != ' ') {
            $('#result').html(" ");
            $.ajax({
                type: 'post',
                url: 'livesearch.php',
                data: { 'search': text },
                success: function(dataReturn) {
                    $('#result').html(dataReturn);
                }
            });
        }
     });
});

And in my PHP
 

$s = mysqli_query($mysql_link, "SELECT * FROM dagenshug_product WHERE product_name LIKE '%".$_POST['search']."%'");
while ($search_result = mysqli_fetch_array($s)) {


    print_r($search_result['product_name']);
    
}
 
Link to comment
Share on other sites

I can make it so it will store every single letter.. but that's not what i want :)

 

So what do you want? Forget about the code and just tell us how the search is supposed to work.

 

If every keystroke triggers a search, it's not possible to reliably tell when the input is complete. You can only guess. For example, save the input after a certain delay or when the text field loses focus due to the user clicking somewhere else.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.