1

I want to fetch my data in a textbox by selecting a dropdown select on my view. now here's my table I have this table which populate my dropdown named aircraft_registration_number and when I select one of its data I need to fetch the number or aircraft_id on which row I select and be fetched on the textbox.

So take a look at my table

enter image description here

and here is my controller

public function findPrice(Request $request){
        $p = Aircraft::select('aircraft_id')->where('id',$request->id)->first();
        return response()->json($p);
    }

my Route

 Route::get('/admin/aircrafts/findPrice', 'Admin\AircraftsController@findPrice');

my View

{{Form::select('aircraft_registration_number', $aircraft_reg,null,['class' => 'form-control-lg productname', 'placeholder' => 'Select RPC No.'])}}<br>
    <br>
    {{Form::text('prod_price', '', ['class' => 'form-control','data-dependent'=>'city'])}}

my AJAX/JQuery

 <script type="text/javascript">

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

            var a=$(this).parent();
            console.log(prod_id);
            var op="";

            $.ajax({
            type:'get',
            url:'{!!URL::to('findPrice')!!}',
            data:{'id':prod_id},
            dataType:'json',//return data will be json
            success:function(data){
                console.log("price");
                console.log(data.price);

                // here price is column name in products table data.coln name

                a.find('.prod_price').val(data.price);

            },
            error:function(){

            }
        });


        });
  });

  </script>

on my console.logs the error was this

enter image description here

1 Answer 1

1

You are targeting the wrong URL.

Change this:

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

to this:

url:'{!! URL::to('admin/aircrafts/findPrice') !!}',

Or:

url: '{!! url('admin/aircrafts/findPrice') !!}',

Edit: To populate your text box, you also need to make sure that you are targeting the right class. Since, in your AJAX callback, you are targeting .prod_price, you also need to add the class to your input.

{{Form::text('prod_price', '', ['class' => 'form-control prod_price','data-dependent'=>'city'])}}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks!!!! the error was gone but the data was not displaying i wonder why.. still the data was being fetch as in id
I don't understand. You could do a console.log() in your AJAX callback to see if you are getting the data correctly and work from there :)
oh no.. :( please help me sir the only problem now is i cannot display the fetch data in ajax to my textbox :(
Edited my answer.
already tried that sir, but still nothing is being displayed on my textbox do you think there's something wrong with the controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.