Codeigniter - Append rows in view then add those rows to database table

孤人 提交于 2020-05-21 07:34:04

问题


I have a form for end users, and in one section of the form they will need to append rows to continue to add their data.

No matter how many rows I append or post, whether it's 2, 5, or 10, only the last row of data makes it to the database.

TABLE STRUCTURE

sub_name            sub_tests         sub_dx
Jimmy's Deli         11                3
Sally's Shop         31                2

APPEND SCRIPT

    <script type="text/javascript">
    $(document).ready(function(){      
      var i=1;  

      $('#add').click(function(){  
           i++;  
           $('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"> <td><input type="text" name="addmore[$i][sub_name]" placeholder="Enter Name" class="form-control name_list" required="" /></td> <td><input type="text" name="addmore[$i][sub_tests]" placeholder="Total Tests" class="form-control name_list" required="" /></td> <td><input type="text" name="addmore[$i][sub_dx]" placeholder="Total Diagnosed" class="form-control name_list" required="" /></td> <td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td> </tr>');  
      });

      $(document).on('click', '.btn_remove', function(){  
           var button_id = $(this).attr("id");   
           $('#row'+button_id+'').remove();  
      });  

    });  
</script>

VIEW

            <div class="table-responsive">  
                <table class="table table-bordered" id="dynamic_field"> 

                  <!-- APPEND -->
                    <tr>  
                        <td><input type="text" name="addmore[$i][sub_name]" placeholder="Enter Name" class="form-control name_list" required="" /></td>
                        <td><input type="text" name="addmore[$i][sub_tests]" placeholder="Total Tests" class="form-control name_list" required="" /></td>
                        <td><input type="text" name="addmore[$i][sub_dx]" placeholder="Total Diagnosed" class="form-control name_list" required="" /></td>

                        <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>  
                    </tr>  
                  <!-- APPEND -->

                </table>  
                <!--input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" /-->  
            </div>

CONTROLLER

// this is posting only the last array to the database table and not the first one.
      foreach ($this->input->post('addmore') as $key => $value)
        {
          $this->input->post['sub_name'];
          $this->input->post['sub_tests'];
          $this->input->post['sub_dx'];
        }
      {
      $this->db->insert('non_clinical_subs', $value);
      }

PRINTED ARRAY

// not sure if I broke the code up the correct way 
[addmore] =>

Array ( [0] => Array ( 
[sub_name] => Row 10:43 - 1 
[sub_tests] => 2 
[sub_dx] => 2 ) ) 

// this part doesn't look right
[sub_name] => Array ( [0] => [1] => ) 
[sub_tests] => Array ( [0] => [1] => ) 
[sub_dx] => Array ( [0] => [1] => ) 

来源:https://stackoverflow.com/questions/59968370/codeigniter-append-rows-in-view-then-add-those-rows-to-database-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!