Jaber Ahmad
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?
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; ?>";
}
}
Thank you for your help
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
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; ?>');">
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
ASKER
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 ...
_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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Try
$("[id^=CHK_coupon_]").eac h(function () {
total += $(this).val() * 1;
});
$("[id^=CHK_coupon_]").eac
total += $(this).val() * 1;
});
The biggest issue was you tried to send the input fields instead of the value of the input fields
ASKER
"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;
});
ASKER
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...
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';
<?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>";}
}
?>
ASKER
I did forget a comma after the url: url
ASKER
Yes yes, I pointed it out earlier in the conversation.
the comma was put though.
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);
}
});
})
});
the code I posted calls 'request/coupons_upp.php' when I uncheck
I assume you changed to 'request/coupons_del.php'
I assume you changed to 'request/coupons_del.php'
ASKER
ASKER
The problem came from this missing line:
const Checked = $parent.find("[id^=CHK_coupon_]".prop("checked");
This line returned me the value of CHK_coupon (300-500-200)const CHK_coupon = this.value;
The condition was not working (at least I believe)const url = Checked ? 'request/coupons_del.php' : 'request/coupons_add.php';
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';
}
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';
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';
ASKER
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
It is now that I saw that you edited the code 1 hour ago in this
Sorry for being heavy
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';
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';
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
ASKER
In any case, thank you very much Michel.
You simplified my code to a much better level! lol
You simplified my code to a much better level! lol
Also why do you reload the page after the ajax?