Support the ongoing development of Laravel.io →
Views Blade Forms
Last updated 2 years ago.
0

If you have a lot of sub categories, pull them in via ajax after sector is selected

If you don't have many sub categories, load them all in and hide/show based on selected sector

0

elite123 said:

If you have a lot of sub categories, pull them in via ajax after sector is selected

If you don't have many sub categories, load them all in and hide/show based on selected sector

Thank you for the reply... I'm new to laravel. Can you guide me to integrate ajax?

0

Finally done it... :)


MyController.php

public function getSubCategories(Request $request, $id){
        if($request->ajax()){

          $sector = Sector::find($id);
          return Response::json( $sector->category );;

        }
    }


Script.js

//get subcateogries according to sector selection
  $("#sector_id").change(function(){
    $.get("submission/getSubCategories/"+ $(this).val(), function(data){

        $element = $("#category_id");
        $element.removeAttr('disabled');

        $(data).each(function(){
          $element.append("<option value='"+ this.id +"'>"+ this.name +"</option>");
        });

    });

  })



Route.php

Route::get('submission/getSubCategories/{id}', 'SubmissionController@getSubCategories'); //AJAX request to get sub categories


0

Sign in to participate in this thread!

Eventy

Your banner here too?

malinga91 malinga91 Joined 21 Jun 2016

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.