i just followed your posted code
Feb 26, 2015
104
Level 14
So...
Here's is your view.
<form>
...
<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"></select>
</div>
</form>
Now, you need to observe country_id select with some JavaScript and send an AJAX request. I'll use jQuery for my example.
$('#country_id').change(function()
{
$.get('/countries/' + this.value + '/cities.json', function(cities)
{
var $state = $('#state');
$state.find('option').remove().end();
$.each(cities, function(index, city) {
$state.append('<option value="' + city.id + '">' + city.name + '</option>');
});
});
});
Now, on your routes file you need to write something like...
get('countries/{code}/cities.json', function($code)
{
return City::where('country_code', $code)->get();
});
1 like
Please or to participate in this conversation.