php - Auto Populate Text Box depending Dropdown List

I want automatically fill some text boxes in a page depending on what the user selects from the dropdown on the same page. so if user changes the dropdown list selection the the text box also change the value depending the dropdown selected.

here is my codes..

My Controller is ProductController.php

 public function getPrice()
   {
      $getPrice = $_GET['id'];
      $price  = Product::all()->where('id', $getPrice)->get();
      return Response::json($price);
   }

Rout

Route::get('getPrice/{id}', 'ProductController@getPrice');

view blade.php is

<table><tr><td>Product</td><td>Qty</td><td>Price</td><td></td></tr>
    <tr><td>
        <select name="productC" id="productC" class="form-control product">
            @foreach($products as $product)
                <option value="{{ $product->name }}" id="{{ $product->id }}" class="productC custom-select">
                    {{ $product->name }}
                </option>
            @endforeach
        </select>
    </td>
    <td width="20%">
        <input type="number" id="qty" min="0" value="0" class="form-control">
    </td>
    <td>
        <input type="text" id="price" />
    </td>
    <td align="right"><input type="submit" class="btn btn-success" name="" value="Add"></td></tr>
    </table>

Ajax Code

<script>
     $(document).ready(function(){
      $('#productC').change(function() {
       var pid =   $(this).find(':selected')[0].id;
        $.ajax({
           type:'GET',
           url:'getPrice/{id}',
           data:{id:pid},
           dataType:'json',
           success:function(data)
             {
               
                 $.each(data, function(key, resp)
                 {     
                  $('#price').text(resp.price);
                });
             }
        });
      });
</script>

Answer

Solution:

Use the below:: $('#price').val(resp.price);

Source