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!