Ivan
Ivan

Reputation: 433

Send ajax - check and confirm

how can i make ajax to check first for result and if any result found to ask for confirm and than to do something ?

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

Controller:

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

Model:

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

Upvotes: 1

Views: 39

Answers (1)

Ivan
Ivan

Reputation: 433

Actually i did it with 2 ajax one that check result and another to confirm

$(".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',
      });
    }
  }
  }
})

Upvotes: 1

Related Questions