Majeed
Majeed

Reputation: 149

how to fetch specific row from database using jquery in laravel and bind it textbox field

i want to fetch specific row data form database based on id using jquery ajax in laravel 5.4,but it does not work. no error from laravel framwork,

this my jquery part.

$(document).ready(function(){
    $('#batch').change(function(){
        var batch_id=$(this).val();
        $.ajax({
            url:"<?php echo route('selectbatch')?>",
            method:'post',
            data:{batch_id:batch_id},
            success:function(data)
            {
                $("#test").html(data.batch);
            }

        });

    });

});

and this is my controller part.

public function selectBatchinfo(Request $request)
{

    $bid=$request->input('batch_id');
    $batchinfo=DB::table('batches')->where('id',$bid)->pluck('s_amt');
    $data = view('register',compact('batches'))->render();
    return response()->json(['batch'=>$data]);

}

Upvotes: 2

Views: 1203

Answers (1)

HirenMangukiya
HirenMangukiya

Reputation: 655

code of your view similar to like this. pass token with post method.

$(document).ready(function(){
$('#batch').change(function(){
    var batch_id=$(this).val();
    $.ajax({
        url:"{{ route('selectbatch')}}",
        method:'post',
        data:{batch_id:batch_id,'_token':"{{csrf_token()}}"},
        success:function(data)
        {
            $("#test").html(data.batch);
        }

    });

});
});

in your controller

public function selectBatchinfo(Request $request)
{
    $bid=$request->batch_id;
    $batchinfo=DB::table('batches')->where('id',$bid)->pluck('s_amt');
    return $batchinfo;
}

in your route file

Route::post('selectbatch','YOUR_CONTROLLER@selectBatchinfo')->name('selectbatch');

try this it may work for you.

Upvotes: 1

Related Questions