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 ?

7
  • please elaborate your question! as what you exactly want to do.. Commented Dec 12, 2016 at 6:19
  • i want to make something like this using codeigniter stackoverflow.com/questions/7340644/… @sheetal Commented Dec 12, 2016 at 6: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? Commented Dec 12, 2016 at 7: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 Commented Dec 12, 2016 at 7:10
  • fine.. you want to add the duplicate dropdown when you select an option from the original dropdown..right? Commented Dec 12, 2016 at 7:40

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.