is it possible to have two values assigned to one checkbox? I require the checkbox to have the value yes or no and I'm simply wondering if this is possible to do with HTML. For example when the checkbox is checked it has the value of 'Yes' and unchecked it has the value of 'No' Thanks
EDIT: Used a workaround that works fine in the PHP that handles the form if anyone else has this issue.
$var1 = e($_POST['checkbox']);
if ($var1 == null){
$var = 'No';
}else{
$var = 'Yes';
}
CodePudding user response:
A checkbox has an attribute 'checked' which you can use to compute the value you need:
const checkbox = document.getElementById('my-checkbox');
const value = checkbox.checked ? 'yes' : 'no';
CodePudding user response:
No, your checkbox will have a value and if it's checked - that value will be submitted with the form, if it's not checked it won't be.
The only way you could have two values is if you had an "onChange" listener in javascript and toggle the value based on if it's checked or not.