普通网友 2018-06-05 07:42
浏览 69
已采纳

CodeIgniter - 将输入字段中的值存储在表格中动态添加到MySQL表中

I have a case I'd like to be solved. I've been learning CI Framework for almost a month now. and what I want to do is I want to store value from input fields in a form that I add dynamically into MySQL table but I have no idea how to do that. I already have the script in HTML + JavaScript for the view.

HTML script

<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

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

is there anyone can help me to pass the value into database from controller? sorry I'm new to CI Framework so please forgive me by asking this kind of question.

  • 写回答

1条回答 默认 最新

  • dougan1205 2018-06-05 07:50
    关注

    use

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

    This will create a form and get data as serializeArray() with jquery then pass that data to the controller with ajax or form submit

    In your controller,

    $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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab