Laravel, jquery Interdependent select lists Full code -


i have got fine until last line of code cannot load select list of view. can't second select list populated (countries regions cities), knackered because seems clear me , dont understand why can't. in firebug able (at html tab) load select list of regions alright, can't send view. might think matter of sending array view controller, won't work because complain variable undefined @ view. cant have variable there waiting value if pregenerated in ajax fashion, can generate @ controller (or in model too) viewable in firebug. enormously appreciate because have gone limit of ability. had working in codeigniter, cant reproduce in laravel. here stuff:

the jquery code

<script type="text/javascript">     jquery(document).ready(function(){        //when first select list changes, function activates         $('#country').change(cargarprovincias); //load regions when countries change      });      function cargarprovincias() {          // collects selected value         var id_pais = $('#country').val();          // sends parameter controller. definitively correct url can see result         $regionsurl = '{{url::route('getregionslist')}}';  //i tried $.post, same results get. had change route accordingly, of course.          $.get( $regionsurl, {'id_pais':id_pais} ) ,function(resp){              //then controller gets value , sends model             // , model retrieve correct result.             // put result string , should picked             // html function here below (inside resp parameter) , fill select lists @ view          $('#regions').empty().html(resp);          };          }         </script>  

here controller:

public function getregionslist(){          $id_pais = input::get('id_pais');                        (new region)->getregion($id_pais);      }  correct id view , gets (if try it, correct feedback model)  here model (it correct results) not echoed @ view    public function getregion($id_pais) {           $regions = db::table('regions')->where('id_pais', $id_pais)->get();           $cadena = "";           foreach ($regions $region){          $cadena .= "<option value ='$region->id_region'>$region->nombre_region</option>";                  }          echo $cadena;          return $cadena;      } 

if example in firebug cadena brings canada, bring this, correct:

<option value ='2055'>alberta</option><option value ='2056'>british columbia</option><option value ='2053'>manitoba</option><option value ='2050'>new brunswick</option><option value ='2047'>newfoundland</option><option value ='2058'>northwest territories</option><option value ='2048'>nova scotia</option><option value ='2057'>nunavut</option><option value ='2052'>ontario</option><option value ='2049'>prince edward island</option><option value ='2051'>quebec</option><option value ='2054'>saskatchewan</option><option value ='2059'>yukon territory</option> 

and view is:

private land

    <select name ="regions" id ="regions" class="form-control">              </select> 

yes, have wished have sent array controller , @foreach $regions ->$region @ view, fail miserably because return variable $regions" undefined, have @ model (or controller) , not have variable waiting values @ view.

instead of using following code:

$id_pais = input::get('id_pais');                (new region)->getregion($id_pais); 

use following code in controller:

$region = region::whereid(input::get('id_pais'))->lists('id_region', 'nombre_region'); return response::json($region); 

then in client side use jquery build dropdown/select, this:

$.getjson($regionsurl, {'id_pais':id_pais}, function(resp) {     $('#regions').empty();     $.each(resp, function(key, value){         var option = $('<option/>', {'id':key, 'text':value});         $('#regions').append(option);     }); }); 

also make sure declare region class/model this:

class region extends eloquent {     protected $table = 'regions';     protected $primarykey = 'id_pais'; // if id_pais primary key     // code... } 

p/s: need learn/read more the documentation.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -