jquery - Search for a table within a database in Laravel 4 PHP Framework

494

I am trying to set up search for a table within database in mine within the Laravel 4 PHP Framework. I am using jquery to accomplish this. I have a table "artists", that I am trying to allow a user to search through. I have a model "Artist.php", and a controller "SearchController.php" that I am using to control the logic. Finally, I have a view "search.blade.php" that I am using as the user facing file. Here is the relevant code:

SearchController.php:

public function show_search() {

    $limit = 10;

        if(isset($_GET['mode']) && !empty($_GET['mode'])) {
            switch($_GET['mode']) {
                case 'autocomplete':
                    if(isset($_GET['keywords']) && !empty($_GET['keywords'])) {

                        $query = htmlspecialchars($_GET['keywords']);
                        $query = mysql_real_escape_string($query);

                        $results = Artist::search_artists($query);

                        $data = array();
                        $i = 0;
                        if(isset($results) && !empty($results)) {
                            foreach($results as $result) {
                                if (strlen(strstr($result->stage_name, 'artists')) == 0) {
                                    if($i < $limit) {
                                        $data[$i] = $result;
                                        $i++;
                                    }
                                }                                                
                            }
                        }
                        exit;
                    }                    
                break;
                }
        }       
        return View::make('search.search');
    }  

Artist.php:

public static function search_artists($query) {
        $search_artists = DB::table('artists')
                        ->where('artists.stage_name', 'LIKE', $query)
                        ->orderBy('created_at', 'DESC')
                        ->get();

        return $search_artists;
    }

search.blade.php:

<input type="text" class="search" id="inputSearch" /><br /> 
<div id="divResult"></div>

<script type="text/javascript">
$(function(){
        $(".search").keyup(function() { 
            var inputSearch = $(this).val();
            var data        = {mode : 'autocomplete', keywords : inputSearch};

            if(inputSearch!='') {
                $.ajax({
                    type: "GET",
                    url: "/search/search",
                    data: data,
                    cache: false,
                    success: function(html) {
                        console.log(html);
                        $("#divResult").html(html).show();
                    }
                });
            }
            return false;
        });
});
</script>

I call all of this with the route:

Route::get('/search/search', array('uses' => '[email protected]_search'));

When I run this and I type things into the search box, I see in the javascript console it reads:

event.returnValue is deprecated. Please use the standard event.preventDefault() instead. 

and the results don't display under the search box. Any idea what could be going wrong? Thank you for your help.

851

Answer

Solution:

The javascript warning is caused by yourreturn false;. If you need it (doesn't seem necessary for thekeyup event unless you want to catch the enter key), you should do something like:

$(".search").keyup(function(e) { 
   e.preventDefault();    // prevent the default action of the event

I am not familiar with Laravel, but it seems to me that you are not getting any results because you are not doing anything with the$data variable in your controller.

I would guess you need something similar to:

return View::make('search.search', $data);

to pass the variable to your view.

People are also looking for solutions to the problem: mysql - How to include a variable inside of a post variable within a query string? -php

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.