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']);
    
}
 
Edited by emptry
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.