我正在一个小型Web应用程序上,我想使用Codeigniter将多个动态字段值插入数据库中,无法将addmore
和type
字段值插入数据库中?我要将单个或多个值插入数据库吗?
HTML
<form name="add_name" method="POST" action="AddMoreController/storePost">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="text" name="addmore[]" placeholder="Enter your Name" class="form-control name_list" required="" /></td>
</tr>
<tr>
<td><input type="text" name="type[]" placeholder="Enter your Name" class="form-control name_list" required="" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
<input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
jQuery
$(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[]" placeholder="Enter your Name" class="form-control name_list" required /></td></tr><tr id="row'+i+'" class="dynamic-added"><td><input type="text" name="type[]" placeholder="Enter your Name" 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();
});
});
控制器
public function storePost()
{
$nameArr = $_POST['addmore'];
$emailArr = $_POST['type'];
if(!empty($nameArr)){
for($i = 0; $i < count($nameArr); $i++){
if(!empty($nameArr[$i])){
$data['name'] = $nameArr[$i];
$data['type'] = $emailArr[$i];
$this->db->insert('tagslist',$data);
}
}
}
print_r('Record Added Successfully.');
}
答案 0 :(得分:1)
更改您的控制器代码:-
public function storePost(){
$nameArr = $_POST['addmore'];
$emailArr = $_POST['type'];
if(!empty($nameArr)){
for($i = 0; $i < count($nameArr); $i++){
if(!empty($nameArr[$i])){
$data = array(
'col_name' => $nameArr[$i],
'col_name' => $emailArr[$i]);
$this->db->insert('tagslist',$data);
}
}
}
print_r('Record Added Successfully.');
}