Hi all,
I have an issue I can't seem to figure out..... I want to use Ajax/jQuery to get data from a database based on what a user inputs in a input box.
So my form is:
<form name="myform" action="" method="get">
<input type="text" name="term" id="amount">
<input type="submit" value="Calculate" id="calc">
</form>
So when the button is clicked it looks to the database table called 'Pricing' and grabs the unit column and returns that full row so i can then show it on the same page within a table.
I haven't tried anything yet as I'm a ajax newbie....
My model is :
class Pricing extends Eloquent {
protected $table = 'pricing';
public $timestamps = false;
}
So how would one do this within a controller and what ajax code would it be to do this without refreshing the page ? Help greatly appreciated.
you should use Form
helper class for creating forms: http://laravel.com/docs/html
second, you must setup a route that will receive the ajax call, ie
Route::post( 'form', 'PricingController@postForm' )
then your ajax code with jquery, would something like this
$.ajax( {
url: '/form',
data: $( 'form' ).serialize(),
method: 'post'
} ).done( function( dataReponse ) {
console.log( dataResponse );
} ).fail( function( xhr, statusCode, msg ) {
console.err( statusCode, msg );
} );
Yeah I will use the form helper class once I have it working.
the data: $('form') part what is that or can i ref an ID #form for example?
lstables said:
Yeah I will use the form helper class once I have it working.
the data: $('form') part what is that or can i ref an ID #form for example?
whatever you like, obviously the selector has to match a single element
please elaborate more, I don't understand that question
Then in my controller method do something like?
$q = Input::get('term');
$pricing = Pricing::where('unit', 'LIKE', '%'. $q .'%');
Or do you suggest something else ?
arcollector said:
please elaborate more, I don't understand that question
There's a done & fail function which I get but how would i render in a view the response? In order to show the data from the selected row to my table?
lstables said:
Then in my controller method do something like?
$q = Input::get('term'); $pricing = Pricing::where('unit', 'LIKE', '%'. $q .'%');
Or do you suggest something else ?
it's ok!
lstables said:
arcollector said:
please elaborate more, I don't understand that question
There's a done & fail function which I get but how would i render in a view the response? In order to show the data from the selected row to my table?
Nope, js works after the page has been completely downloaded and rendered, so you cannot render again the view, for that thing you need to refresh the page, but it's non sense, because if you need to refresh the page after the user has trigger an action, like a form post, then don't use ajax
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community