Forum

Ajax for onChange of select box with Laravel (change with multiple source object...

Any example for ajax of onChnage of select box that change multiple source object like textbox1, textbox2....?

yuomtheara
yuomtheara
  • 3 years ago

this is how i do it

controller

    public function getPositions($id)
    {
        if (Request::ajax())
        {
            $positions = DB::table('sports_positions')->select('position_id','name')->where('sport_id', '=', $id)->get();
            return Response::json( $positions );
        } 
    }

ajax

  /* Load positions into postion <selec> */
  $( "#sport_id" ).change(function() 
  {
    $.getJSON("/sport/"+ $(this).val() +"/positions", function(jsonData){
        select = '<select name="position" class="form-control input-sm " required id="position" >';
          $.each(jsonData, function(i,data)
          {
               select +='<option value="'+data.position_id+'">'+data.name+'</option>';
           });
        select += '</select>';
        $("#position").html(select);
    });
  });

Thanks you so much @extjac , Your sample help me a lot for select option content with ajax. :)

Thank you also! This is a really good example!

You can also follow bellow good example link : http://itsolutionstuff.com/post/laravel-5-dynamic-dependant-select-box-using-jquery-ajax-example-part-1example.html