从ajax表单提交获取空值

时间:2019-10-29 16:28:46

标签: php jquery ajax codeigniter

当我提交from时,我使用ajax从表单中获取空值,在当前情况下,我确实有3个字段,但我没有从表单中获取任何值, 请帮助我找出解决方案。

<table class="table table-hover table-centered m-0 table-bordered">
  <thead>
    <tr>
      <th>Sl</th>
      <th style="width:10%;">name</th>
      <th>email</th>
      <th>Date</th>
    </tr>
  </thead>
  <tbody id="purchaseData">
    <tr>
      <td>1</td>
      <td><input type="text" class="form-control" id="quantity" name="name" placeholder="name" required=""></td>
      <td><input type="text" class="form-control" id="costPrice" name="email" placeholder="500" required=""></td>
      <td><input type="date" class="form-control" id="costPrice" name="email" placeholder="email" required=""></td>
      <td>
        <button type="button" class="btn btn-primary item-edit" id="btnSave">Submit</button>
      </td>
    </tr>
    <tr>
      <td>1</td>
      <td><input type="text" class="form-control" id="quantity" name="name" placeholder="name" required=""></td>
      <td><input type="text" class="form-control" id="costPrice" name="email" placeholder="500" required=""></td>
      <td><input type="date" class="form-control" id="costPrice" name="date" required=""></td>
      <td><button type="button" class="btn btn-primary item-edit" id="btnSave">Submit</button></td>
    </tr>
  </tbody>
</table>
$(function() {
  $('#purchaseData').on('click', '.item-edit', function() {
    var row = $(this).closest("tr")
    var name = row.find("[name=name]").val();
    var email = row.find("[name=email]").val();
    var date = row.find("[name=date]").val();

    $.ajax({
      type: "POST",
      url: "<?php echo site_url('Con_test/add_purchase_for_shop')?>",
      dataType: "JSON",
      data: {
        'name': name,
        'email': email,
        'date': date
      },
      success: function(data) {
        if (data == '1') {
          var btn = $(this).closest("tr")
          var name = inputData.find("button").prop('disabled', false).css(
            "background-color", "green");
        } else {
          var btn = $(this).closest("tr")
          var name = inputData.find("button").prop('disabled', true).css(
            "background-color", "#2d7bf4");
        }
      }
    });
  });
});

1 个答案:

答案 0 :(得分:0)

您的日期名称为name =“ email”,应为name =“ date”:

  $(document).ready(function() {
       $('#purchaseData').on('click', '.item-edit', function(event) {

         event.preventDefault();
         var row = $(this).closest("tr")
         var name = row.find("[name=name]").val();
         var email = row.find("[name=email]").val();
         var date = row.find("[name=date]").val();
         console.log('row ', row)
         console.log('name ', name)
         console.log('email ', email)
         console.log('date ', date)
});
    })