【问题标题】:Handling input data on a table using ajax, jquery and codeigniter使用 ajax、jquery 和 codeigniter 处理表上的输入数据
【发布时间】:2015-12-03 08:24:24
【问题描述】:

我的数据库中有这样一个基于 json 的查询:

[{
"NAMA_ITEM": "Certificate",
"NAMA_DAMAGE": "Broken",
"NAMA_REPAIR": "Blast&Paint",
"REMARKS": "AAAAAA",
"MANHOUR": "10.00",
"MATERIAL": null,
"AC": null,
"NAMA_TYPE": "Cleaning"
}, {
"NAMA_ITEM": "Exterior",
"NAMA_DAMAGE": "Modified",
"NAMA_REPAIR": "Replace",
"REMARKS": "BBBBB",
"MANHOUR": "10.00",
"MATERIAL": null,
"AC": null,
"NAMA_TYPE": "Cleaning"
}]

这个json,我会在html上这样解释:

<div class="box-footer">
   <button class="btn btn-block btn-info" type="button" id="klikBiaya">Klik Isi Biaya</button>
</div>

那些json我会显示在一个基于.click jquery的html表上,

$(document).on("click", "#klikBiaya", function () {

    $(this).attr("disabled", true);
    $.ajax({
        url: "<?php echo base_url('admin/c_admin/get_one_eir_to_cost'); ?>",
        type: "POST",
        data: {
            SELECTED: $(this).val()
        },
        dataType: 'json',
        success: function (obj) {
            console.log(obj);
            $.each(obj, function (i, elem) {
                $("#tableReport").find('tbody').append(
                        "<tr><td>" + "<input type='text' class='form-control' name='name_type' disabled placeholder='" + elem.NAMA_TYPE + "'value='" + elem.NAMA_TYPE + "'>" +
                        "<td>" + "<input type='text' class='form-control' name='name_item' disabled placeholder='" + elem.NAMA_ITEM + "'value='" + elem.NAMA_ITEM + "'>" +
                        "<td>" + "<input type='text' class='form-control' name='name_damage' disabled placeholder='" + elem.NAMA_DAMAGE + "'value='" + elem.NAMA_DAMAGE + "'>" +
                        "<td>" + "<input type='text' class='form-control' name='name_repair' disabled placeholder='" + elem.NAMA_REPAIR + "'value='" + elem.NAMA_REPAIR + "'>" +
                        "<td>" + "<input type='text' class='form-control' name='name_remarks' disabled placeholder='" + elem.REMARKS + "'value='" + elem.REMARKS + "'>" +
                        "<td>" + "<input type='text' class='form-control' name='name_damage' disabled placeholder='" + elem.MANHOUR + "'value='" + elem.MANHOUR + "'>" +
                        "<td>" + "<input type='text' class='form-control' id='material'>" +
                        "<td>" + "<input type='text' class='form-control' id='A/C'>" +
                        "</td></tr>");
            });
        }
    });

});

看,我的表格现在有两行,但在每一行,但有两个输入文本用户必须填写,有 MATERIAL AND A/C.

如何处理提交时的这两个输入? 我想更新我的数据库中的一些数据。 这是html:

<form id="upload" action="">
<div class="box-body">
    <div class="row">
        <div class="col-xs-12">
            <div class="box-body table-responsive no-padding">
                <table class="table table-hover" id="tableReport">
                    <thead>
                    <th>TYPE</th>
                    <th>ITEM</th>
                    <th>DAMAGE</th>
                    <th>REPAIR</th>
                    <th>REMARKS</th>
                    <th>MANHOUR</th>
                    <th>MATERIAL</th>
                    <th>A / C</th>

                    </thead>

                    <tbody>
                        <!--GENERATED BY JQUERY-->
                    </tbody>
                </table>
            </div><!-- /.box-body -->
        </div>
    </div>
</div><!-- /.box-body -->

<div class="box-footer">
    <button class="btn btn-primary" id="submitCost">Submit</button>
</div>

我应该在 jquery ajax 中做什么。我应该将什么数据传递给控制器​​。我想根据上面的 json 更新我的数据库。 JQUERY ON VIEW

$(document).on("submit", "#upload", function () {
    // ARRAY OR JSON THAT INPUT FROM USER

    $.ajax({
        url: "<?php echo base_url('surveyor/c_admin/update_json_detail'); ?>",
        type: "POST",
        data: {
            POST_ARRAY: // ARRAY OR JSON THAT INPUT FROM USER
        },
        dataType: 'json',
        success: function (obj) {
            console.log("UPDATE IS SUCCESS");
        }
    });
    return false;
});

控制器

public function update_json_detail(){
   $this->input->post("ARRAY_POST");
   $query =  $this->m_admin->update_eir_to_cost(element, element, element);
   echo json_encode($query);
}

型号

 public function update_eir_to_cost($id, $material, $ac) {
    $data = array(
        "MATERIAL" => $material,
        "AC" => $ac
    );

    $this->db->trans_start();
    $this->db->where($id);
    $this->db->update('tb_repair_detail', $data);
    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE) {
        // generate an error... or use the log_message() function to log your error
        echo "Error Updating";
    }
}

【问题讨论】:

    标签: javascript jquery json ajax codeigniter


    【解决方案1】:

    在你的 jquery 中试试这个:

    $(document).on("submit", "#upload", function () {
       postData = $(this).serialize();
        $.ajax({
            url: "<?php echo base_url('surveyor/c_admin/update_json_detail'); ?>",
            type: "POST",
            data: postData,
            dataType: 'json',
            success: function (obj) {
                console.log("UPDATE IS SUCCESS");
            }
        });
        return false;
    });
    

    【讨论】:

    • var postInput = $(this).serialize(); console.log(postInput);给我一个结果:一个空字符串
    【解决方案2】:

    .click jquery

    换行

    "<td>" + "<input type='text' class='form-control' id='A/C'>"
    

    "<td>" + "<input type='text' class='form-control' id='ac'>"
    

    id 指定元素的唯一 ID。命名规则:

    Must contain at least one character
    Must not contain any space characters
    In HTML, all values are case-insensitive
    

    JQUERY ON VIEW:

    $(document).on("submit", "#upload", function (e) {
      e.preventDefault();
    
      $.ajax({
        url: "<?php echo base_url('surveyor/c_admin/update_json_detail'); ?>",
        type: "POST",
        data: {
          material: $('input#material').val(),
          ac: $('input#ac').val()
        },
        dataType: 'json',
        success: function (json) {
          if (json['error']) {
            console.log("Error!");
          } else {
            console.log("UPDATE IS SUCCESS");
          }
        }
      });
    
      return false;
    });
    

    控制器

    public function update_json_detail(){
      $id = <row id>;
      $material = $this->input->post("material");
      $ac = $this->input->post("ac");
      $query =  $this->m_admin->update_eir_to_cost($id, $material, $ac);
    
      header('Content-Type: application/json');
      echo json_encode($query);
    }
    

    型号

    public function update_eir_to_cost($id, $material, $ac) {
      $data = array(
        "MATERIAL" => $material,
        "AC" => $ac
      );
    
      $this->db->trans_start();
      $this->db->where($id);
      $this->db->update('tb_repair_detail', $data);
      $this->db->trans_complete();
    
      $_response = array();
    
      if ($this->db->trans_status() === FALSE) {
        $_response = array('error' => true, 'message' => 'Your message here ....');
      } else {
        $_response = array('success' => true, 'message' => 'Your message here ....');
      }
    
      return $_response;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 2020-05-13
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多