为什么我的代码使用Ajax成功将数据保存到数据库,但所有行都是空白?
我的表格:
<form name="frm" id="frm" action="">
<div class="form-group">
<label for="namaproduk">Nama Produk</label>
<input type="text" class="form-control" id="namaproduk" placeholder="Nama Produk">
<!--<small class="form-text text-muted">We'll never share your email with anyone else.</small>-->
</div>
<div class="form-group">
<label for="unitproduk">Segment/Unit</label>
<select class="form-control" id="unitproduk">
<option value="1">Cabang</option>
<option value="2">Mikro</option>
<option value="3">SME</option>
</select>
<!--<small id="namaprodukhelp" class="form-text text-muted">We'll never share your email with anyone else.</small>-->
</div>
</form>
<div class="col-sm-12 alert" >
<button type="button" name="saveproduk" id="insertproduk" class="btn btn-lg btn-primary pull-right"><span class="fa fa-save"> </span> Save Product</button>
</div>
我的Ajax:
$("#insertproduk").on('click', function (e) {
e.preventDefault();
var DataString=$("#frm").serializeArray()
$.ajax({
type: 'POST',
url: '<?php echo base_url();?>saveproduct',
data: DataString,
success: function (data) {
//jQuery("#attendence_report_holder").html(response);
alert("success");
},
error:function(data){
alert("failed");
}
});
});
我的控制器:
public function saveproduct(){
$this->product_m->saveproduct_m();
}
我的模特:
function saveproduct_m(){
$dataproduk = array(
"namaproduct" => $this->input->post("namaproduk"),
"idunit" => $this->input->post("unitproduk"));
echo $result = $this->db->insert("tbl_product",$dataproduk);
}
答案 0 :(得分:2)
由于您尚未为字段定义name
属性,因此将其保存为空白。与id="namaproduk"
一起,您还应该分别为所有字段name="namaproduk"
。
所以它应该是:
<input type="text" class="form-control" id="namaproduk" name="namaproduk" placeholder="Nama Produk">
和
<select class="form-control" id="unitproduk" name="unitproduk">