动态输入值不会保存在带有codeigniter的数据库中 [英] Dynamic input values doesn't save in the databsae with codeigniter

查看:68
本文介绍了动态输入值不会保存在带有codeigniter的数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

I want letting user add dynamic inputs and save those values in the database. But with this code only one value save to the database. How can I save all values to the database entered by user this is my view to add dynamic inputs

    <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> 

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

This is my controller to save those dynamic inputs in the database

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');

}
    }


  } 


推荐答案

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');
        }
    }
  }

这篇关于动态输入值不会保存在带有codeigniter的数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆