Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar

@rohit i dont have any idea about javascript if you help me i glad you!

vipin93's avatar

@dinis have you get your problem solved? if still not can you show me your Route of your Form

rohit's avatar

@vipin93 try this

CountriesController.php

public function getIndex($country_id=null) {
    $countries = get_all_countries_from_your_model;
    $states = array();
    if($country_id != null) {
        $states = get_all_states_from_your_model_using_country_id;
    }
    return View::make('index', [“countries” => $countries, ‘states’ => $states]);
}

Index view

<select id="countries-dd" name="countries-dd" class="form-control" onchange="loadStatesForCountryWithId(this.value);">
    <option value="-1">Select country</option>
    @foreach($countries as $country)
        <option value="{{ $country->country_id }}">{{ $country->country_name }}</option>
    @endforeach
</select>

@if(sizeof($states) != 0)
    <select id=“states-dd" name=“states-dd" class="form-control">
        <option value="-1">Select state</option>
        @foreach($states as $state)
            <option value="{{ $state->state_id }}">{{ $state->state_name }}</option>
        @endforeach
    </select>
@endif

<script type=“text/javascript”>
    function loadStatesForCountryWithId(country_id) {
        window.location.href = url_to_your_page/country_id
    }
</script>

I've written this on the fly just at the top of my head. There may be some minor errors. But I hope you understand where I'm going with this.

JackD's avatar

@vipin93 here is my script

` $('#country').on('change',function(e){ //state change console.log(e); var cat_id = e.target.value;

$.get('ajax-subcat?cat_id=' + cat_id, function(data){

    $('#state').empty();

    $.each(data, function(edit, subcatObj){

        $('#state').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');

    });
});

});

here is my view `

                    <div class="form-group">
                        <lablel for="country_id">Select Country:</lablel>
                        <select name="country_id" id="country" class="form-control">
                            @foreach($countries as $country )
                                <option value="{{ $country->countryCODE }}">{{ $country->countryNAME }}</option>
                            @endforeach
                        </select>
                    </div>



                    <div class="form-group">
                        <lablel for="state">Select State:</lablel>

                        <select name="state" id="state" class="form-control">

                            <option value=""></option>

                        </select>
                    </div>

                </form>

here is my route `Route::get('ajax-subcat', function(){

$cat_id = Input::get('cat_id');

$states = City::where('cityCOUNTRYCODE', '=', $cat_id)->get();

return Response::json($states);

});

JackD's avatar

@vipin93 im working inside default register page with that one where i add country and city selection

vipin93's avatar

@dinis you have three mistake in javascript chnage id to countryCODE and name to countryNAME and one thing //state change delete it

vipin93's avatar

@dinis

$('#state').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');

change only id and name not subcatObj.id subcatObj.name

JackD's avatar

@vipin93 here is my script but still the same

`

    $('#country').on('change',function(e){
        console.log(e);
        var cat_id = e.target.value;
        $.get('ajax-subcat?cat_id=' + cat_id, function(data){

            $('#state').empty();

            $.each(data, function(edit, subcatObj){

                $('#state').append('<option value="'+subcatObj.countryCODE+'">'+subcatObj.countryNAME+'</option>');

            });
        });

    });





</script>
vipin93's avatar

@dinis where is your controller how you fetching country and what mean of countryCode is that same to id and where is you fetching city or state

JackD's avatar

here is my route

`Route::get('ajax-subcat', function(){

$cat_id = Input::get('cat_id');

$states = City::where('cityCOUNTRYCODE', '=', $cat_id)->get();

return Response::json($states);

});

JackD's avatar

yes i can see the list of country in my first selection but when i select country no city is being shown

vipin93's avatar

@rohit i'm geting error Uncaught ReferenceError: loadStatesForCountryWithId is not defined

JackD's avatar

i have that i think already

here is my controller

` public function getRegister() { $genders = Gender::all(); $countries = Country::all(); $cities = City::all();

    return view('auth.register',compact("genders","countries","cities"));
}
vipin93's avatar

@dinis

public function getRegister() 
{ 
 $genders = Gender::all(); 
$countries = Country::all(); 

    return view('auth.register',compact("genders","countries"));
}
JackD's avatar

why should i remove cities??

here is what im getting after checking the browser console

`Object { originalEvent: change, type: "change", isDefaultPrevented: bb(), timeStamp: 1424970971903000, jQuery1112004522455806518644: true, which: undefined, view: undefined, target: <select#country.form-control>, shiftKey: undefined, relatedTarget: undefined, 10 more… }

vipin93's avatar

Bcz in route you already mentioned

`Route::get('ajax-subcat', function(){

$cat_id = Input::get('cat_id');

$states = City::where('cityCOUNTRYCODE', '=', $cat_id)->get();

return Response::json($states);

});

Did you using jquery or not

RomainLanz's avatar

If you want to still RESTful you can have a route like this

Route::get('countries/{id}/cities', function() {
    // Do what you need to do.
});

I can't see where is your problem. You just need to send an AJAX request to a URL and use return values to create your new select.

Please or to participate in this conversation.