1

I use Codeigniter and bootstrap.

I have 2 select name service and sub-service (both values are in the array) like this:

enter image description here

I'd like to select the service in the first select, on the basis of this select get the related sub-service...

example of arrayOfService (service a, service b, service c) example of

arrayOfSubservice = (
    [0] => stdClass Object
        (
            [nome] => nomine ed incarichi RSPP
            [service] => a
        )

    [1] => stdClass Object
        (
            [nome] => DVR
            [service] => a
        )

    [2] => stdClass Object
        (
            [nome] => ART 36 Informazione
            [service] => b
        )

my goal is if its possibile, select the "service a" in the first select, automatically view in the subservce select ONLY the subservice related to "service a"

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
riccardo airone
  • 506
  • 1
  • 6
  • 21
  • maybe you find answers here: https://stackoverflow.com/questions/4480637/how-to-change-a-selections-options-based-on-another-select-option-selected – Vickel Feb 27 '20 at 00:07
  • @Vickel my select are created dinamically not in html but in php – riccardo airone Feb 27 '20 at 00:11
  • I know, that's why I didn't mark your question as a duplicate, but you get the idea from there how to implement this, right? on change of first -> populate second – Vickel Feb 27 '20 at 00:41
  • @Vickel i dont have any idea :( – riccardo airone Feb 27 '20 at 00:43
  • another hint: https://stackoverflow.com/questions/18526914/how-can-i-populate-a-dropdown-list-by-selecting-the-value-from-another-dropdown. Search for *dropdown list populate by selecting value from parent dropdown* or similar – Vickel Feb 27 '20 at 00:46
  • @Vickel the problem is that im a newbie – riccardo airone Feb 27 '20 at 01:03
  • @riccardoairone if you are a newbie you can check my code here you will get an idea https://github.com/eboominathan/Dependent-Dropdown-in-Codeigniter-3.0.3 – Boominathan Elango Feb 27 '20 at 04:32

1 Answers1

0

Since the sub-service select box is dependent you can't pass both the values at once to the view. I assume the data source is a database table. So you can make an asynchronous call to the controller by passing in the service ID.

Please refer to the below code which could give you some idea to incorporate the functionality.

Jquery Code

$(".your-first-selectbox-class").change(function(){
   var value = $.trim($(this).val());
   if(value != ""){
      $.ajax({
        url: base_url+"your_controller/get_value",  
        type:"POST",
        cache: false,               
        data:{id:value},
        success: function(respond){
            $(".get-change").html(respond);
        }
     });
   }
});

Once the data is posted to the controller method, you can retrieve the sub-service values as follows.

function get_value(){
   $ido = $this->input->post('id');
   //here you will get the sub-services based on the service ID you are passing in
   $data = $this->your-model-name->get_records($ido);
   $option = "";

   //traverse through the resultset to create option string.
   foreach($$res as $row){
      $option .= "<option value='".$row->ID."'>".$row->name."</option>";
   }
   echo $option;
}

I hope that helps you.

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28