发送ajax-检查并确认

时间:2019-04-05 07:13:47

标签: php jquery ajax

我该如何使Ajax首先检查结果,以及是否发现任何结果要求确认并做某事?

$(".btn-delete").click(function(){
var url = "index.php?route=catalog/product/delete&user_token={{user_token}}";
var id = $('input[name*=\'selected\']:checked');
$.ajax({
  url: url,
  data: id,
  dataType: 'json',
  success: function(data){
  }
});
});

控制器:

if (isset($this->request->post['selected']) && $this->validateDelete()) {
        foreach ($this->request->post['selected'] as $product_id) {
            $this->model_catalog_product->deleteProduct($product_id);
        }

型号:

public function deleteProduct($product_id) {
    $checkPreporuchaniProducts = $this->db->query("SELECT product_id FROM oc_preporuchani_category");
    $checkid = array();
    foreach ($checkPreporuchaniProducts->rows as $PreporuchaniProducts) {

        $getId = unserialize($PreporuchaniProducts['product_id']);

        foreach ($getId as $id) {
        if ($product_id == $id) {
            echo "yes: $id<br>";
        }else {
            echo "no: $id<br>";
        }
        }
    }

1 个答案:

答案 0 :(得分:1)

实际上,我用2个Ajax做到了,一个检查结果,另一个检查确认

$(".btn-delete").click(function(){
var urlcheck = "index.php?route=catalog/product/checkBeforeDelete&user_token={{user_token}}";
var urldelete = "index.php?route=catalog/product/delete&user_token={{user_token}}";
var urldeleteSpecial = "index.php?route=catalog/product/deleteSpecial&user_token={{user_token}}";
var id = $('input[name*=\'selected\']:checked');
$.ajax({
  url: urlcheck,
  data: id,
  type: 'POST',
  dataType: 'json',
  success: function(data){
    if (data.delete == null) {
    if (confirm("This product is not used you can delete it")) {
      $.ajax({
        url: urldelete,
        data: id,
        type: 'POST',
        dataType: 'json',
      });
    }
  }else {
    if (confirm("!!! WARNING: Product is used as SPECIAL if delete will effect other system as well !!!")) {
      $.ajax({
        url: urldeleteSpecial,
        data: id,
        type: 'POST',
        dataType: 'json',
      });
    }
  }
  }
})