使用Ajax插入是成功的,但是数据库行是空的

时间:2022-09-26 08:46:55

Why does my code successfully save data to database using Ajax, but all rows are blank?

为什么我的代码使用Ajax成功地将数据保存到数据库中,但所有的行都是空的?

My Form:

我的表格:

<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">&nbsp;</span> Save Product</button>
</div>

My Ajax :

我的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");
    }
  });
}); 

My Controller:

我的控制器:

public function saveproduct(){
   $this->product_m->saveproduct_m();
}

My Model:

我的模型:

function saveproduct_m(){ 
    $dataproduk = array(
    "namaproduct" => $this->input->post("namaproduk"),
    "idunit" => $this->input->post("unitproduk"));
    echo $result = $this->db->insert("tbl_product",$dataproduk);
}

1 个解决方案

#1


2  

It's saving as blank because you haven't defined name attribute for your fields. Along with id="namaproduk" you should also have name="namaproduk" for all your fields respectively.

它保存为空白,因为您还没有为字段定义name属性。除了id="namaproduk"之外,您还应该为所有字段分别拥有name="namaproduk"。

So it should be:

所以它应该是:

<input type="text" class="form-control" id="namaproduk" name="namaproduk" placeholder="Nama Produk">

And

<select class="form-control" id="unitproduk" name="unitproduk">

#1


2  

It's saving as blank because you haven't defined name attribute for your fields. Along with id="namaproduk" you should also have name="namaproduk" for all your fields respectively.

它保存为空白,因为您还没有为字段定义name属性。除了id="namaproduk"之外,您还应该为所有字段分别拥有name="namaproduk"。

So it should be:

所以它应该是:

<input type="text" class="form-control" id="namaproduk" name="namaproduk" placeholder="Nama Produk">

And

<select class="form-control" id="unitproduk" name="unitproduk">