Back

Ajax for onChange of select box with Laravel (change with multiple source object like textbox1, textbox2, ....)?


yuomtheara posted 3 years ago

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

extjac replied 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);
    });
  });
yfly replied 3 years ago

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

rodvan replied 2 years ago

Thank you also! This is a really good example!

savanihd replied 9 months ago

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


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us