2

I'm trying to use Ajax in Laravel to have search results populate the "search_results_div" div without having to leave the page.

Right now I'm getting this error message: "Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from orders where `` LIKE %%)".

So it looks like orders is getting sent but nothing else is. This is my first time with Ajax so while the code is not working I don't understand where I'm going wrong, so any help or pointers would be very much appreciated! Thanks!

The search_column and search_input are added to the form via JS after search_table has an input. I have hard-coded what this will look like for simplicity's sake and have also tried the code after hard-coding the inputs in and received the same error message. All the same I thought I'd point this out as well.

Here is my View:

 <div class="large-6 columns">
{{Form::open( array('route' => 'search', 'method' => 'post', 'id' => 'search_form'))}}      

    {{ Form::select('search_table',
    array(
            'select'        => 'Search...',
            'orders'        => 'Search orders',
        ), null, array('id' => 'search_table')) }}

<div class="search_box" id="search_column_div">
    <select id='search_column'>
    <option value='created_at'>by date created</option>     
</select>
</div>

<div id="search_input_div">
    <input id='search_input' class='search_input' placeholder='Enter Order Name'/>
</div>

{{ Form::submit('Search', array('id' => 'search_button','class' => 'button expand radius search_button no_display'))}}
    {{ Form::close() }}
</div>

      <div id="search_results_div">
      </div>
</div>

My JS (My guess is here is where the problem is):

 /*AJAX Search*/
$('#search_form').click(runSearch);

var runSearch = function(e){
    e.preventDefault();
        $('form#search_form').submit(function(){
            $.ajax(
                {
                url: '/search',
                type: 'post',
                cache: false,
                dataType: 'json',
                search_table: $('#search_table').val(),
                success: function(data){
                    console.log("Success!");
                    $('#search_results_div').htlm('Yes');
                },
                error: function(xhr, textStatus, thrownError){
                    alert('Somethin went wrong');
                }
            });
        });
};

And my Controller:

public function searchPost(){
$search_table   = Input::get('search_table');
$search_column  = Input::get('search_column');
$search_input   = Input::get('search_input');

$search = DB::table($search_table)->where($search_column, 'LIKE', "%{$search_input}%")->get();
return $search;

}

And my Route:

/*Search Page for Ajax*/
    Route::post('/search', array(
        'as'    => 'search',
        'uses'  => 'HomeController@searchPost'
    ));

Thanks again for any input or advice!

mario
  • 1,503
  • 4
  • 22
  • 42
  • 2
    Your JS doesn't submit any data. –  May 13 '14 at 17:43
  • Lol I'm still trying to get it to just not crash. But to that point I thought that with the "success" section that I could tell it to pass the returned data (data) into the "search_results_div"'s html. Am I going about this all wrong? – mario May 13 '14 at 17:45

1 Answers1

3

Your form's select doesn't have a search_column name attribute, which is the name that Laravel uses to identify the field when using Input::get().

Use something like this :

<select id="whatever" name="search_column">

As for the Javascript, try this code :

$('#search_form').click(runSearch);

var runSearch = function(e){
    e.preventDefault();
    $('form#search_form').submit(function(){
        $.ajax({
            url: '/search',
            type: 'post',
            cache: false,
            data: $('#search_form').serialize(),
            success: function(data){
                console.log("Success!");
                $('#search_results_div').html(data);
            },
            error: function(xhr, textStatus, thrownError){
                alert('Somethin went wrong');
            }
        });
    });
};

First you don't need to post it in JSON format, the default URL-encoded format is more than enough and works just fine with Laravel's Input::get("variable") (I'm not even sure if it actually accepts JSON at all).

Next, the actual data wasn't present in your code, so your request was empty and obviously Laravel wasn't happy because all three input variables were empty and the DB query was failing (by the way, you should use validation to check if the input is present and if not return a nice & clean error message instead of failing completely).

The data variable is set to serialize() your form, it "converts" your form into an URL-encoded query string.

Finally the success function sets the HTML content of search_results_div to the data returned by Laravel (you should return a view there, currently you just return JSON).

I suggest you also look at some of these questions, they may contain code that you can fix using the answers provided and then reuse :

Community
  • 1
  • 1
  • Thanks, I tried the code and am still getting the "SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from `orders` where `` LIKE %%)" error message. – mario May 13 '14 at 17:59
  • Also thanks for the explanation, still really new to this ajax stuff so every bit helps. – mario May 13 '14 at 17:59
  • *facepalm. Ugh. Lol. Well I corrected that as well but I'm still getting the "SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from `orders` where `` LIKE %%)" message – mario May 13 '14 at 18:06
  • One thing is the the fields are added by js as the user fills one. So originally only the search_table field is there but each one is added by js after the previous is filled and it ends up looking like what I have in the post. I have already tried hardcoding everything in and I still get the same error message – mario May 13 '14 at 18:12
  • 1
    Your `select` only has an `id` attribute, but in URL-encoded form data, fields are identified by their `name` attribute. –  May 13 '14 at 18:14
  • That got it. At least I think so. Now I'm getting tons of stuff back lol. Pages and pages of info and it looks like its all the orders that match the search query, but ALL of the info for ALL of the matches lol. Thanks though! – mario May 13 '14 at 18:19
  • If you put that as an answer I'll be happy to accept it. – mario May 13 '14 at 18:19