1

I have two dropdown City and City Area. I want to fill City area drop-down based on the selected value of City.

Route:

  Route::get('create-profile', 'ProfileController@index');
  Route::get('create-profile/city_area/{id}', 'ProfileController@city_area');

Primary Dropdown is : City

<div class="col-xs-7 col-md-push-1">
<select name="city" id="city">
<option value="">--- Select City ---</option>
 @foreach($city as $city)
  <option value="{{$city->id}}">{{$city->city_name}}</option>
 @endforeach
 </select></div>

Dependent Dropdown is

     <div class="inp_sec">
      <div class="col-xs-4">
      <label for="r3">City Area</label>
      </div><div class="col-xs-7 col-md-push-1">
      <select name="city_area" id="city_area"></select></div></div>

Ajax for dependend dropdown is

<script type="text/javascript">
$(document).ready(function() {
    $('select[name="city"]').on('change', function() {
        var cityID = $(this).val();
        if(cityID) {
            $.ajax({
                url: 'create-profile/city_area/'+cityID,
                type: "GET",
                dataType: "json",
                success:function(data) {
                    $('select[name="city_area"]').empty();
                    $.each(data, function(key, value) {
                        $('select[name="city_area"]').append('<option value="'+ key +'">'+ value +'</option>');
                    });
                }
            });
        }else{
            $('select[name="city_area"]').empty();
        }
    });
});

Php Code to fetch data of dependent dropdown is

public function city_area($id)
{
$city_area= DB::table('czech_area')->where('citi_id',$id)- 
>select('id','area_name')->get();
    return json_encode($city_area);
}

1 Answer 1

1

When you see [object object], it's a clear sign of an object-to-string conversion.

city_area() function returns list of objects, that you later encode with JSON. Therefore, the value variable you use to fill city_area dropdown is a Javascript object, not a string. You need to specify the properties of this object you want to use - id and area_name in your case:

$('select[name="city_area"]').append('<option value="'+ value.id +'">'+ value.area_name +'</option>');
Sign up to request clarification or add additional context in comments.

10 Comments

if I am using thing, dependent dropdown is showing, 'undefined' values for all rows
Could you paste what "data" variable contains? The one returned from the call to city_area
by the way, you shouldn't have to encode JSON manually in city_area - Laravel will do that automatically for you
on selection first dropdown its returning [{"id":22,"area_name":"Praha 2"},{"id":21,"area_name":"Praha 1"}] (without json) and [{"id":22,"area_name":"Praha 2"},{"id":21,"area_name":"Praha 1"}] (with json)
Do you get only one value in the city_area dropdown or more? Could you console.log value in the $.each callback?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.