表格连续循环提交?

时间:2017-09-23 02:13:48

标签: javascript php codeigniter

这是我的观点: product_information.php

<form id="form-product-info" data-parsley-validate class="form-horizontal form-label-left">
            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12" for="product-SKU">SKU <span class="required">*</span>
              </label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input type="text" id="product-SKU" required="required" class="form-control col-md-7 col-xs-12" value="<?php echo $results[0]->SKU; ?>">
              </div>
            </div>

            <div class="ln_solid"></div>
            <div class="form-group">
              <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                <button class="btn btn-primary" type="button" id="btn-customer-list">Cancel</button>
                <button class="btn btn-danger" type="button" id="btn-delete-product">Delete</button>
                <button type="submit" class="btn btn-success source">Submit</button>
              </div>
            </div>

          </form>

这是我的控制器: product.php

public function updateProductInformation()
    {
        $product_information['name'] = $this->input->post('name');
        $product_information['SKU'] = $this->input->post('SKU'); 
        $product_information['product_id'] = $this->input->post('id');

        $product_information['last_updated'] = date('Y-m-d H:i:s');

        var_dump($product_information);
        $update = $this->updateProductInformation($product_information);

        $this->update($update);
    }

JavaScript文件: product.js

$('#form-product-info').submit(function() {
  alert('Submitting form');
  var id = $('#product-name').data('product-id');
  updateProductInformation(id);
});

function updateProductInformation(id)
{
  alert('Updating product information ' + id);
  var name  = $('#product-name').val();
  var SKU = $('#product-SKU').val();
  alert(name);
  alert(SKU);
  alert(id);  

  $.ajax({
    type: 'post',
    url: base_url + 'product/updateProductInformation',
    data: {
      'name' : name,
      'SKU' : SKU,
      'id' : id
    },
    success: function(msg)
    {
      if (msg == 'true')
      {
        alert('Updating product information successful');
      }
      else
      {
        alert("Please try again. ");
      }
    },
    error: function(jqXHR, textStatus, errorThrown)
    {
      console.log('Response text: ' + jqXHR.responseText);
      console.log('Status code: ' + textStatus);
      console.log('Error thrown: ' + errorThrown);
    }
  });
}

当我点击提交时,控制台中会抛出一个错误。当我在一个新选项卡中打开它时,我会得到一个连续的流:

array(4) { ["name"]=> NULL ["SKU"]=> NULL ["product_id"]=> NULL ["last_updated"]=> string(19) "2017-09-23 10:07:20" }

直到最终内存耗尽,我才会收到致命的错误。我似乎无法找到导致这种情况的原因。我设置的警报只显示一次。就我检查而言, updateProductInformation(id)仅分配给一个事件处理程序。即使是从其他地方调用它,警报也应该不止一次显示。

1 个答案:

答案 0 :(得分:0)

找到了罪魁祸首:

 $update = $this->updateProductInformation($product_information);

这应该是:

$update = $this->Product_model->updateProductInformation($product_information);

很容易错过!