动态输入值不会保存在带有codeigniter的数据库中

时间:2017-07-24 05:21:01

标签: javascript codeigniter

this is my interface this is my database我想让用户添加动态输入并将这些值保存在数据库中。但是使用此代码只有一个值保存到数据库中。如何将所有值保存到用户输入的数据库中 这是我添加动态输入的观点

    <script type="text/javascript">
    $(document).ready(function() {
        var max_fields      = 10; //maximum input boxes allowed
        var wrapper         = $(".input_fields_wrap"); //Fields wrapper
        var add_button      = $(".add_field_button"); //Add button ID

        var x = 1; //initlal text box count
        $(add_button).click(function(e){ //on add input button click
            e.preventDefault();
            if(x < max_fields){ //max input box allowed
                x++; //text box increment
                $(wrapper).append('</br><div><input class="input form-control"" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
            }
        });

        $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
    });
    </script>
<div class="input_fields_wrap">
                    <div class="form-group">
                    <button type="button" class="btn btn-success add_field_button">Add More Fields</button> 
                    </div>
                    <div>
                    <input class="input form-control" name="mytext[]">
                    </div>
                </div> 

这是我的控制器,用于在数据库中保存这些动态输入

function error(){


  if ($this->input->post('mytext')) { 
    $attain = $this->input->post('mytext', true);


    foreach ($attain as $i => $a) { // need index to match other properties
        $data2 = array(
            'mytext' => $a,
            'projectname'                   => $this->input->post('projectname'),

        );

        $this->db->insert('projectem', $data2); 
        redirect('Select_ctrl2/ModalAddEmployeesProject');

}
    }


  } 

1 个答案:

答案 0 :(得分:0)

function error(){
    if ($this->input->post('mytext')) { 
        $attain = $this->input->post('mytext', true);
        $data2=array(); //<-initialize
            foreach ($attain as $i => $a) { // need index to match other properties
                //append array
                $data2[] = array(
                'mytext' => $a,
                'projectname'=> $this->input->post('projectname'),
            );
            //for multiple entry in same table
            $this->db->insert_batch('projectem', $data2); 
            redirect('Select_ctrl2/ModalAddEmployeesProject');
        }
    }
  }