codeigniter-store以表单形式动态添加到mysql表中的输入字段值

aor9mmx1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(316)

我有个案子要解决。我学习ci框架已经快一个月了。我想做的是,我想把输入字段中的值以一种我动态添加到mysql表中的形式存储起来,但我不知道怎么做。我已经有了视图的html+javascript脚本。
html脚本

<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div>
    <input type="text" name="mytext[]" placeholder="Account Title">
    <input type="text" name="mytext2[]" placeholder="Description">
    <input type="text" name="mytext3[]" placeholder="Credit">
    <input type="text" name="mytext4[]" placeholder="Debit">
</div>
</div>

javascript脚本

<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('<div><input type="text" name="mytext[]" placeholder="Account Title"><input type="text" name="mytext2[]" placeholder="Description"><input type="text" name="mytext3[]" placeholder="Credit"><input type="text" name="mytext4[]" placeholder="Debit"><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>

有没有人能帮我把值从控制器传给数据库?很抱歉,我是新来的ci框架,所以请原谅我问这样的问题。

r8xiu3jd

r8xiu3jd1#

使用

<form method="POST" id="myform">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div>
    <input type="text" name="field[]" placeholder="Account Title">
    <input type="text" name="field[]" placeholder="Description">
    <input type="text" name="field[]" placeholder="Credit">
    <input type="text" name="field[]" placeholder="Debit">
</div>
</div>
</form>

这将创建一个表单,并使用jquery以serializearray()的形式获取数据,然后使用ajax或formsubmit将数据传递给控制器
在你的控制器里,

$input_data=$this->input->post();
//now input data has an array "field"  You can use that data,
you will see result array like Array ( [field] => Array ( [0] => fsdf [1] => dsfdsf [2] => dsf [3] => dsfds ) )
//$this->db->insert("target_table",$input_data);//this will insert a record into target_table in database

相关问题