Pushing data to a html list for search using ajax, jquery and php, codeigniter

498

First I want to show you guys what I am trying to do.
I am trying to create a search functionality in my application using AJAX, jquery and php and as for the framework I am using Codeigniter, but it's not necessary to be CI, this could be similar to all(well I suppose).

I have this piece of code to observe afocus andblur event.

$("#searchbox").on({
    keyup : debounce(function(){
        MSI.Interface.search();
        },350,false),
    blur  : function(){
        $("#search_results").hide();
        }
});

I have not finished it yet that is why theblur event has only.hide() . I can't think of what other else to include, maybe reset thehtml of the#search_results to make it blank, but I don't know if that is reasonable.

1st question: What do you think is a more reasonable solution for that?

As you can see in the previous code, i have the debounce function, just to prevent per character request on the server, I wonder if that is correct.

Then I have thissearch function

search : function() {
    var keyword = $("#searchbox").val();
    if (keyword == '') {
    } else {
        $.ajax({
            url : MSI.variables.base_url + 'search',
            type: 'POST',
            data: {
                keyword : keyword
            },
            dataType : 'json',
            success: function(output) {
                $.each(output, function() { 
                    $.each(this, function(key, value){
                        $("#search_results").show().prepend("<p>"+value+"</p>");
                    });
                });
            }
        });
    }
}

With that code, the script will be able to add contents to the#search_results div.

2nd Question: What do you think is a better solution for this?

People are also looking for solutions to the problem: php - Removing object from array; how to avoid nulls

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.