@rohit i dont have any idea about javascript if you help me i glad you!
@dinis have you get your problem solved? if still not can you show me your Route of your Form
@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.
@vipin93 no not yet im still not getting any after selecting country
@dinis can you show me your route(get) for get Form
@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);
});
@vipin93 im working inside default register page with that one where i add country and city selection
@dinis change your country_id to country in your view page
@vipin93 still the same
@dinis you have three mistake in javascript chnage id to countryCODE and name to countryNAME and one thing //state change delete it
@vipin93 sorry where is that?
$('#state').append('<option value="'+subcatObj.id+'">'+subcatObj.name+'</option>');
change only id and name not subcatObj.id subcatObj.name
@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>
@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
my controller is in my route
@dinis plz show me
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);
});
@dinis where you fetching Country
from my database
@dinis I mean in your controller or in route show me
here is the code in my view @foreach($countries as $country ) countryCODE }}">{{ $country->countryNAME }} @endforeach
@dinis did you see your country in your country option in view
yes i can see the list of country in my first selection but when i select country no city is being shown
Thats by i'm saying you show me your controller or route where your register form get
If you still not try it from start as in this link http://laravel.io/bin/yGYwo
@rohit i'm geting error Uncaught ReferenceError: loadStatesForCountryWithId is not defined
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"));
}
public function getRegister()
{
$genders = Gender::all();
$countries = Country::all();
return view('auth.register',compact("genders","countries"));
}
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… }
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
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.