简体   繁体   English

使用ajax,jquery和codeigniter处理表上的输入数据

[英]Handling input data on a table using ajax, jquery and codeigniter

I have a json like this based query on my database : 我在数据库上有一个基于此查询的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"
}]

This json, I will be interprated on html like this : 这个json,我将像这样在html上插入:

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

Those json I will be displayed on a html table based .click jquery, 这些json我将显示在基于.click的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>");
            });
        }
    });

});

See, my table now have two row but on each row, but there are have two input text that the user must to fill it, there is MATERIAL AND A/C. 看到,我的表现在有两行,但每一行都有,但是用户必须填写两个输入文本,其中有“物料”和“ A / C”。 图片

How to handle on those two input on submit ? 如何处理提交的那两个输入? I want to update some data on my database. 我想更新数据库中的一些数据。 this is the html : 这是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>

What I am supposed to do in jquery ajax. 我应该在jquery ajax中做什么。 What the data I am supposes to passed it into controller. 我想将什么数据传递给控制器​​。 I want to update my database based the json above. 我想基于上面的json更新我的数据库。 JQUERY ON VIEW : 查看JQUERY

$(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;
});

CONTROLLER 控制器

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);
}

MODEL 模型

 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";
    }
}

In Your jquery try this : 在您的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;
});

.click jquery .click jQuery

Replace line 更换线

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

To

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

id Specifies a unique id for the element. id指定元素的唯一ID。 Naming rules: 命名规则:

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

JQUERY ON VIEW : 查看JQUERY:

$(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;
});

CONTROLLER 控制器

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);
}

MODEL 模型

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM