我发现在codeigniter中运行动态表单存在问题,我以动态形式提交数据后出现问题,所有数据都无法将数据保存到数据库SQL中,有关详细信息,请在此处查看我的代码:
观点:
<html>
<head>
<title>Form Data</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h2 align="center">Personal Information</h2>
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" style="width:100px;"/></td>
<td><input type="text" name="age[]" placeholder="Enter your Age" class="form-control name_list" style="width:100px;"/></td>
<td><input type="remark" name="remark[]" placeholder="Remark" class="form-control name_list" style="width:100px;"/></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" style="width:100px;"/></td><td><input type="text" name="age[]" placeholder="Enter your Age" class="form-control name_list" style="width:100px;"/></td><td><input type="remark" name="remark[]" placeholder="Remark" class="form-control name_list" style="width:100px;"/></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();
});
$('#submit').click(function(){
$.ajax({
url:"<?php echo base_url(); ?>dashboard_admin/save_item",
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
</script>
控制器:
public function save_item()
{
$number = count($this->input->post["name"]);
if($number > 0)
{
for($i=0; $i<$number; $i++)
{
if(trim($this->input->post["name"][$i] != ''))
{
$in['name'] = mysqli_real_escape_string($this->input->post["name"][$i]);
$in['age'] = mysqli_real_escape_string($this->input->post["age"][$i]);
$in['remark'] = mysqli_real_escape_string($this->input->post["remark"][$i]);
$this->db->insert("tbl_item",$in);
}
}
echo "Data Inserted";
}
else
{
echo "Please Enter Name";
}
}
我已经尝试过运行此代码,但没有任何反应,如果我的源代码有任何更正,我需要您的建议,谢谢。
答案 0 :(得分:0)
您必须先制作一个数组,然后再插入批处理。
public function save_item() {
$insertarr = array();
for($i=0; $i<count($this->input->post["name"]); $i++){
$insertarr[] = array(
'name' => mysqli_real_escape_string($this->input->post["name"][$i]),
'age' => mysqli_real_escape_string($this->input->post["age"][$i]),
'remark' => mysqli_real_escape_string($this->input->post["remark"][$i])
);
}
$this->db->insert_batch('tbl_item', $insertarr);
}