Iam trying to retrieve data from database based on one select box(vendor_location) another select box(vendor_area) must be filled using ajax and json in laravel. This is my script code:

Posted 1 year ago by AbdulBazith
   $(document).ready(function()

    {   

             $(document).on('change','.ven_loc',function(){      
       
                var ven_loc=$(this).val();        

                $.ajax({ 
                   
                     type:'get',

                     url:'{!! URL::to('fetch') !!}',

                     data:{'ven_loc':ven_loc}, 

                     success:function(data)

                     {

                         if(data)

                            { 

                             $("#ven_area").empty();

                             $("#ven_area").append('<option>Select area</option>');

                             $.each(data,function(key,value)

                              {

                             $("#ven_area").append('<option value="'+key+'">'+value+'</option>'); 
                   
                            });

                        }

                        },

                            error:function()

                         {

                             console.log('fail');

                     }

                        });

});

});

And this is my retrieve code in controller

function fetch(Request $request)

{

$data = Location_details::where("ven_loc",$request->ven_loc)->select('ven_area')->get();

return response()->json($data);

}

But it displaying [object Object] in the second select box(vendor_area). The areas of the selected locations must be displayed. I need the value to be displayed in the the second select box(vendor_area). can any one help?

Please sign in or create an account to participate in this conversation.