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

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

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


    <form method="POST" id="myform">
    <div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
        <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">

    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,

    //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
    解决 无用
    打赏 举报

相关推荐 更多相似问题