0

i have array dropdown form on codeigniter,i want to add another form when one of array is selected .this is what i tried so far

**Form**

        <label class="col-sm-2 control-label">Item Name</label> 

    <?php
          $js='id="item" onChange="additem();"';
          $options = array(
                            '1' => 'Metal',
                            '2' => 'Plastic',
                            '3' => 'Rubber',
                            '4' => 'Glass',
                          );

              echo form_dropdown('item', $class,$js);

      ?>



Model

public function get_class()
{
    $this->db->select('id_metal, desc_metal');
    $this->db->from('uip_metal');
    $result = $this->db->get();
    if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
            $return[$row['id_metal']] = $row['desc_metal'];
        }
    }
    return $return;
}
same function for the 3 remaining item

Controller

public function add() 
{       
    $data['uip_inventaris'] = $this->uip_inventariss->add();
    $data['action']  = 'uip_inventaris/save';
    $data['metal']=$this->uip_inventariss->get_metal();
    $data['plastic']=$this->uip_inventariss->get_plastic();
    $data['tekukbesi']=$this->uip_inventariss->get_rubber();
    $data['class']= $this->uip_inventariss->get_glass();

}

how can i make the javascript code ?

Bakti Wijaya
  • 447
  • 1
  • 6
  • 21
  • please elaborate your question! as what you exactly want to do.. – sheetal Dec 12 '16 at 06:19
  • i want to make something like this using codeigniter http://stackoverflow.com/questions/7340644/dynamically-add-input-type-select-with-options-in-javascript @sheetal – Bakti Wijaya Dec 12 '16 at 06:43
  • okay...so do you just want to add duplicate of the dropdown when one of the option is selected or you want to add a different dropdown as per the option selected? – sheetal Dec 12 '16 at 07:08
  • i want to add duplicate dropdown...i already tried to make javascript code for it,it seems it doesnt work,or i get wrong at passing the id @sheetal – Bakti Wijaya Dec 12 '16 at 07:10
  • fine.. you want to add the duplicate dropdown when you select an option from the original dropdown..right? – sheetal Dec 12 '16 at 07:40
  • it is...but somehow,i figured out how to populate the dropdown list from database.and still,confuse at making the javascript @sheetal – Bakti Wijaya Dec 12 '16 at 09:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130390/discussion-between-sheetal-and-bakti-wijaya). – sheetal Dec 12 '16 at 09:17

1 Answers1

0

You don't need to shuffle through the model or controller classes for this, you can just use javascript or jquery to clone/duplicate the dropdown[created using form_dropdown()] when one of the option from dropdown is selected. For this, just add the code below to your view:

 <label class="col-sm-2 control-label">Item Name</label> 
 <div id="itemSelect">
    <?php
        $js='id="item"';
        $options = array(
                        '1' => 'Metal',
                        '2' => 'Plastic',
                        '3' => 'Rubber',
                        '4' => 'Glass',
                    );
        echo form_dropdown('item', $class,$js);
    ?>
 </div>
 <script type="text/javascript">
    $(function(){
      $(document.body).on('change','select[name="item"]',function(){
        $(this).clone().appendTo($('#itemSelect'));
      });
    });
 </script>
sheetal
  • 565
  • 6
  • 13