我使用以下视图代码来创建用户的动态输入。我的框架是codeigniter。
<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('</br><div><input class="input form-control"" name="mytext[]"/><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>
<div class="input_fields_wrap">
<div class="form-group">
<button type="button" class="btn btn-success add_field_button">Add More Fields</button>
</div>
</div>
我的控制器将用户输入保存在数据库
function error(){
$attain = $this->input->post('mytext', true);
$data2=array(); //<-initialize
foreach ($attain as $i => $a) { // need index to match other properties
//append array
$data2[] = array(
'mytext' => $a,
'projectname'=> $this->input->post('projectname'),
);
//for multiple entry in same table
$this->db->insert_batch('projectem', $data2);
}
redirect('Select_ctrl2/ModalAddEmployeesProject');
}
当用户添加动态输入时,它无法正常保存。某些值在数据库处重复。任何人都可以帮助我吗?