Link to home
Start Free TrialLog in
Avatar of Jaber Ahmad
Jaber AhmadFlag for Côte d'Ivoire

asked on

Uncaught RangeError

Hello experts!

When I want to add or edit my table with this function, I encounter a slowness and an error on $.ajax ({});
Uncaught RangeError: Maximum call stack size exceeded
This only happens when I put this ajax.
Can you help me fix it please?

function coupon(i) {
   var CHK_coupon = document.getElementById("CHK_coupon_" + i);
   var HAS_coupon = document.getElementById("HAS_coupon_" + i);
   var VAL_coupon = document.getElementById("VAL_coupon_" + i);
   var TOT_coupon = document.getElementById("TOT_coupon");
    var DIV = document.getElementById("montant_coupon");
    var NIC_handle = "<?php echo $_SESSION_HANDLE; ?>";
    
   if(CHK_coupon.checked == true) {
        TOT_coupon.value = VAL_coupon.value;

        $.ajax({
            type : "POST",
            url: 'request/coupons_add.php',
            cache: false,
            data: {HAS_coupon:HAS_coupon, VAL_coupon:VAL_coupon, NIC_handle:NIC_handle},
            error: function(e){console.log('Ajax Error',e);alert('Erreur Ajax !');},
            success: function(response){location.reload(true);}
        });
        
    } else {
        TOT_coupon.value = null;
        
            $.ajax({
            type : "POST",
            url: 'request/coupons_upp.php',
            cache: false,
            data: {HAS_coupon:HAS_coupon, VAL_coupon:VAL_coupon, NIC_handle:NIC_handle},
            error: function(e){console.log('Ajax Error',e);alert('Erreur Ajax !');},
            success: function(response){location.reload(true);}
        });
    }
}

<?php echo 'var max = '.$i.';'; ?>

   window.addEventListener("DOMContentLoaded", function(event) {for(var i=0; i < max; i++) {
      document.getElementById("CHK_coupon_" + i).addEventListener("change", Gift);
   }});
   
function Gift() {
  var total = 0;
   for(var i=0; i < max; i++) {
     if(document.getElementById("CHK_coupon_" + i).checked) {
       total += document.getElementById("CHK_coupon_" + i).value * 1;
    }
        TOT_coupon.value = parseFloat(total / <?php echo MNY_TAUX; ?>).toFixed(0);
        montant_coupon.innerHTML = "-" + parseFloat(total / <?php echo MNY_TAUX; ?>).toFixed(0) + " <?php echo MNY_PAIRE; ?>";
  }

}

Open in new window

Thank you for your help
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

When and where do you call coupon

Also why do you reload the page after the ajax?
Avatar of Jaber Ahmad

ASKER

When I click on the checkbox, it should save the record in the table.
So I use the onclick so that if (CHK_coupon.checked == true) {it will execute the coupons_add.php page

<input type="checkbox" id="CHK_coupon_<?php echo $i; ?>" onclick="coupon('<?php echo $i; ?>');">

Open in new window

The refresh is done because the value must be hard recalculated when the initial page is reloaded.
There is likely something you are calling onload that you did not show
No not really, I tried removing the location.reload (true) as well; but he does the same.

_coupons.php

Loading the page just retrieves the elements from my table in PHP.
Just a select from table and display the values in the fields, really nothing special what ...
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help.
The TOT_coupon (and montant_coupon) is a NaN (instead of 700 in my example) and no injection is done in the table.
I added a "," after VAL_coupon: VAL_coupon, which was also missing.

User generated imageIn any case your code is very short (I am ashamed)

Try

$("[id^=CHK_coupon_]").each(function() {
      total += $(this).val() * 1;
    });
The biggest issue was you tried to send the input fields instead of the value of the input fields
It directly shows me 1000 as total and still does not save in the table ...
User generated imageAh yes I see what you mean ... sorry for the blunder.
"save in the table" ? You mean in the 'request/coupons_add.php' ??? I do not know that code
SORRY about the total:

$("[id^=CHK_coupon_]").each(function() {
  if (this.checked) total += this.value * 1;
});

Open in new window

The javascript code works perfectly, thank you very much for this great improvement in my code.

However the request/coupons_add.php does not work.
I put values in the table manually and tried to deactivate a coupon and the record was deleted...

const url = CHK_coupon.checked ? 'request/coupons_add.php' : 'request/coupons_del.php';

Open in new window


<?php require_once($_SERVER["DOCUMENT_ROOT"]."/sv-includes/config.php");

var_dump($_POST);

if (isset($_POST['HAS_coupon'])) {
$HAS_coupon = $_POST['HAS_coupon'];
$NIC_handle = $_POST['NIC_handle'];
$VAL_coupon = $_POST['VAL_coupon'];

try{$AD = $pdo->prepare("INSERT INTO tab_shop_coupons (hash, nic_handle, valeur) VALUES (:HAS_coupon, :NIC_handle, :VAL_coupon)");
$AD ->execute(array(
   "HAS_coupon"   => $HAS_coupon,
   "NIC_handle"   => $NIC_handle,
   "VAL_coupon"   => $VAL_coupon
));
}catch(PDOException $e){echo "<div class='alert alert-danger'>".$e->getMessage()."</div>";}

}
?>

Open in new window

I don't know if it can help but I did a
         console.log (response);
         console.log (url);
and the only file called is request/coupons_del.php
User generated image
I did forget a comma after the url: url
Yes yes, I pointed it out earlier in the conversation.
the comma was put though.

$(function() {

  $("[id^=CHK_coupon_]").on("change", function() {
  
    const $parent    = $(this).closest(".card");
    const CHK_coupon = this.value;
    const HAS_coupon = $parent.find("[name^=HAS_coupon_]").val();
    const NIC_handle = "<?php echo $_SESSION_HANDLE; ?>";
    const VAL_coupon = $parent.find("[name^=VAL_coupon_]").val();
    const DIV = document.getElementById("montant_coupon");
    
    const url = CHK_coupon.checked ? 'request/coupons_add.php' : 'request/coupons_del.php';

    let total = 0;
    $("[id^=CHK_coupon_]").each(function() {
      if (this.checked) total += $(this).val() * 1;
    });
      
    $("#TOT_coupon").val(parseFloat(total / <?php echo MNY_TAUX; ?> ).toFixed(0));
    $("#montant_coupon").html("-" + parseFloat(total / <?php echo MNY_TAUX; ?> ).toFixed(0) + " <?php echo MNY_PAIRE; ?>");


    $.ajax({
      type: "POST",
      url: url,
      cache: false,
      data: {
        HAS_coupon: HAS_coupon,
        NIC_handle: NIC_handle,
        VAL_coupon: VAL_coupon
      },
      error: function(e) {
        console.log('Ajax Error', e);
        alert('Erreur Ajax !');
      },
      success: function(response) {
        //location.reload(true);
        console.log(response);
        console.log(url);
      }
    });
  })
});

Open in new window

Here is code that looks like it could work

https://jsfiddle.net/mplungjan/ubwcj026/
the code I posted calls 'request/coupons_upp.php' when I uncheck

I assume you changed to 'request/coupons_del.php'
When I reverse the request/coupon_ whether the CHK_coupon is checked or not, it executes the same file...

const url = CHK_coupon.checked ? 'request/coupons_del.php' : 'request/coupons_add.php';

Open in new window

User generated image
The problem came from this missing line:
const Checked = $parent.find("[id^=CHK_coupon_]".prop("checked");

Open in new window

This line returned me the value of CHK_coupon (300-500-200)
const CHK_coupon = this.value;

Open in new window

The condition was not working (at least I believe)
const url = Checked ? 'request/coupons_del.php' : 'request/coupons_add.php';

Open in new window

So I did this:
  $("[id^=CHK_coupon_]").on("change", function() {
  
    const $parent    = $(this).closest(".card");
    const CHK_coupon = this.value;
    const Checked    = $parent.find("[id^=CHK_coupon_]").prop("checked");
      
    const HAS_coupon = $parent.find("[name^=HAS_coupon_]").val();
    const NIC_handle = "<?php echo $_SESSION_HANDLE; ?>";
    const VAL_coupon = $parent.find("[name^=VAL_coupon_]").val();
    const DIV = document.getElementById("montant_coupon");
    
    //const url = Checked ? 'request/coupons_del.php' : 'request/coupons_add.php';
    
    if(Checked == true) {
        var url = 'request/coupons_add.php';
    }
      else {
        var url = 'request/coupons_del.php';
    }

Open in new window

What do you think ?
I had changed my code to

   const url = this.checked ? 'request/coupons_add.php' :  'request/coupons_upp.php';


after I changed to

const CHK_coupon = this.value;

so please double check my code from the fiddle

And you changed

'request/coupons_upp.php';

to

'request/coupons_del.php';
Yes, I changed 'request/coupons_upp.php'; to 'request/coupons_del.php';
the name page and in the code.

I didn't pay attention to your edit, as soon as you posted this
const url = CHK_coupon.checked ? 'request/coupons_add.php' : 'request/coupons_del.php';

Open in new window

I automatically copied it.
It is now that I saw that you edited the code 1 hour ago in this
const url = this.checked ? 'request/coupons_add.php' : 'request/coupons_del.php';

Open in new window

Thank you very much for your help, everything is okay now.
Sorry for being heavy
No problem - I changed a lot and should have made sure you were aware of every change
In any case, thank you very much Michel.
You simplified my code to a much better level! lol