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