Skip to content Skip to sidebar Skip to footer

How To Retrieve Data Using Ajax And Without Going To Post Page? Laravel 5

I'm new to using ajax. For example after field title is filled, I want to search in database for specific data and return more fields based on that input. So far I can only receive

Solution 1:

What you need is to implement the jquery keypress function.

so here is you js:

$("input.title").keypress(function(){
    var title = $(this).val();

    // now do the ajax request and send in the title value
    $.get({
        url: 'url you want to send the request to',
        data: {"title": title}, 
        success: function(response){
           // here you can grab the response which would probably be // the extra fields you want to generate and display it
        }
    });
});

as far as in Laravel you can pretty much treat it the same as a typical request except you will return json:

Route::get('/url-to-handle-request', function({
    // lets say what you need to populate is 
   //authors from the title andreturn them

   $title = Route::get('title'); // we are getting the value we passed in the ajax request

    $authors = Author::where('title' ,'=', $title)->get();

    return response()->json([
        'authors' => $authors->toArray();
    ]);
}));

Now I would probably use a controller and not just do everything within the route but I think you'll get the basic idea.

Post a Comment for "How To Retrieve Data Using Ajax And Without Going To Post Page? Laravel 5"