PCHomepage
Programmer
I wrote a simple function to uncheck a checkbox when the other is checked. If neither is selected, then I expected it to submit nothing but it is apparently submitting 0. I purposely didn't use radio buttons in order to be able to deselect both in a pair which can't be done with radio buttons and I did not want to add a third choice for null.
However, that's not the question I have. There are two pairs of checkboxes but I combined the function for both. If I select opt0 or opt2, opt1 or opt3 are deselected as expected but if I then try to select opt1 or opt3, nothing happens until I manually uncheck opt0 or opt2. Since I am a PHP programmer and rarely use JavaScript, I am at a loss as to the problem and I can't spot any typos so can someone suggest a cure? A second set of eyes looking it over would be most appreciated.
The HTML is partially database-driven and includes:
However, that's not the question I have. There are two pairs of checkboxes but I combined the function for both. If I select opt0 or opt2, opt1 or opt3 are deselected as expected but if I then try to select opt1 or opt3, nothing happens until I manually uncheck opt0 or opt2. Since I am a PHP programmer and rarely use JavaScript, I am at a loss as to the problem and I can't spot any typos so can someone suggest a cure? A second set of eyes looking it over would be most appreciated.
JavaScript:
<script type="text/javascript">
function setChecked(){
var opt0 = document.getElementById("opt0");
var opt1 = document.getElementById("opt1");
var opt2 = document.getElementById("opt2");
var opt3 = document.getElementById("opt3");
if (opt0.checked){
opt1.checked = false;
} else if (opt1.checked){
opt0.checked = false;
}
if (opt2.checked){
opt3.checked = false;
} else if (opt3.checked){
opt2.checked = false;
}
}
</script>
The HTML is partially database-driven and includes:
Code:
<label for="OffSite">Off Site:</label>
<?php $OffyesChecked = ($rowStaff['OffSite'] == 1) ? " CHECKED" : "";?>
<?php $OffnoChecked = ($rowStaff['OffSite'] == 0 && $rowStaff['OffSite'] != NULL) ? " CHECKED" : "";?>
<input type="checkbox" name="OffSite" id="opt1" value="1" <?=$OffyesChecked?> onclick="setChecked(this);">yes
<input type="checkbox" name="OffSite" id="opt0" value="0" <?=$OffnoChecked?> onclick="setChecked(this);">no
<label for="Club">Club:</label>
<?php $ClubyesChecked = ($rowStaff['Club'] == 1) ? " CHECKED" : "";?>
<?php $ClubnoChecked = ($rowStaff['Club'] == 0 && $rowStaff['OffSite'] != NULL) ? " CHECKED" : "";?>
<input type="checkbox" name="Club" id="opt3" value="1"<?=$ClubyesChecked?> onclick="setChecked(this);">yes
<input type="checkbox" name="Club" id="opt2" value="0"<?=$ClubnoChecked?> onclick="setChecked(this);">no