<form method="get" action="">
<input type="checkbox" name="ch1"/>
<input type="hidden" name="ch1-1" value="100"/>
<input type="checkbox" name="ch2"/>
<input type="hidden" name="ch2-1" value="200"/>
<input type="submit"/>
</form>
<?php
$ch1 = isset($_GET['ch1']) ? $_GET['ch1-1'] : 0;
$ch2 = isset($_GET['ch2']) ? $_GET['ch2-1'] : 0;
echo "Result " . ((int)$ch1 + $ch2);
?>
lub
<form method="get" action="">
<input type="checkbox" name="ch1" value="100"/>
<input type="checkbox" name="ch2" value="200"/>
<input type="submit"/>
</form>
<?php
$ch1 = $_GET['ch1'] ?? 0;
$ch2 = $_GET['ch2'] ?? 0;
echo "Result " . ((int)$ch1 + $ch2);
?>
Pozwolę sobie z'edytować post.
Skopiuj HTML z drugiego przykładu i napisz tak.
let sum = 0;
for(let i=0;i<document.querySelectorAll('form input[type=checkbox]').length;i++) {
console.log(1);
document.querySelectorAll('form input[type=checkbox]')[i].addEventListener('change', function(e) {
if(document.querySelectorAll('form input[type=checkbox]')[i].checked) {
sum+=(document.querySelectorAll('form input[type=checkbox]')[i].value|0);
} else {
sum-=(document.querySelectorAll('form input[type=checkbox]')[i].value|0);
}
console.log(sum);
}
, false)
};
document.querySelectorAll('form input[type=checkbox]')..., możesz przypisać do zmiennej.