POST unchecked HTML checkboxes
Add a hidden input for the checkbox with a different ID:
<input id='testName' type='checkbox' value='Yes' name='testName'>
<input id='testNameHidden' type='hidden' value='No' name='testName'>
Before submitting the form, disable the hidden input based on the checked condition:
form.addEventListener('submit', () => {
if(document.getElementById("testName").checked) {
document.getElementById('testNameHidden').disabled = true;
}
}
How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML
Simplest one, no javascript required, just put a hidden input before the checkbox:
<input type="hidden" name="check[0]" value="0" />
<input type="checkbox" name="check[0]" value="1" />
Inputs need to have the same name. If the checkbox is checked then value 1 will be submitted, otherwise value 0 from the hidden input.
Your case javascript solution, no hidden inputs needed:
<script type="text/javascript">
// when page is ready
$(document).ready(function() {
// on form submit
$("#form").on('submit', function() {
// to each unchecked checkbox
$(this + 'input[type=checkbox]:not(:checked)').each(function () {
// set value 0 and check it
$(this).attr('checked', true).val(0);
});
})
})
</script>
<form method="post" id="form">
<input type="checkbox" name="check[0]" value="1" />
<input type="checkbox" name="check[1]" value="1" />
<input type="submit" value="Save Changes" />
</form>
PHP solution, no hidden inputs needed:
<?php
// if data is posted, set value to 1, else to 0
$check_0 = isset($_POST['check'][0]) ? 1 : 0;
$check_1 = isset($_POST['check'][1]) ? 1 : 0;
?>
<form method="post">
<input type="checkbox" name="check[0]" value="1" />
<input type="checkbox" name="check[1]" value="1" />
<input type="submit" value="Save Changes" />
</form>
EDIT: the javascript solution is not valid anymore as of jquery 1.6. Based on this, a more proper solution is the following:
<script type="text/javascript">
// when page is ready
$(document).ready(function() {
// on form submit
$("#form").on('submit', function() {
// to each unchecked checkbox
$(this).find('input[type=checkbox]:not(:checked)').prop('checked', true).val(0);
})
})
</script>
<form method="post" id="form">
<input type="checkbox" name="check[0]" value="1" />
<input type="checkbox" name="check[1]" value="1" />
<input type="submit" value="Save Changes" />
</form>
Why does an unchecked checkbox have a value of 'on'?
The value of a checkbox is constant. It doesn't change (unless you change it with JavaScript). If you don't specify a value, it defaults to on
.
The value has nothing to do with whether the input is checked or not.
In a traditional form submission, only successful controls will be submitted and checkboxes are not successful unless they are checked. On the server you determine if they are checked or not based on if the value is submitted or not.
You don't get different values with the input depending on its checked state.
If you want to determine if a checkbox is checked or not in client side JavaScript, look at the checked
property, not the value
property.
document.querySelectorAll("input").forEach(input => console.log({
value: input.value,
checked: input.checked
}));
<input type="checkbox" value="on">
<input type="checkbox" value="off" checked>
<input type="checkbox">
<input type="checkbox" value="foo" checked>
Checkbox is not Checked as checkboxes are working in html
Your fiddle and your posted code are not the same. I think your code in fiddle is better and easier to fix. You can do something like:
function uncheckOthers(ele) { if (ele.checked && ele.value == "4") { $('.example').prop('checked', false); //If none is clicked. Uncheck all example } else { $('.termcls').prop('checked', false); //Else, Uncheck termcls since one of the example is clicked }};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="checkbox" onchange="uncheckOthers(this)" class="example" name="Option 1" value="1">Option 1<br><input type="checkbox" onchange="uncheckOthers(this)" class="example" name="Option 2" value="2">Option 2<br><input type="checkbox" onchange="uncheckOthers(this)" class="example" name="Option 3" value="3">Option 3<br><input type="checkbox" onchange="uncheckOthers(this)" class='termcls' name="None" id="none" value="4">None<br>
Trying to set default checkbox value if not checked
Could you not do something like this?
$checkbox = isset($_POST['post_friend']) ? $_POST['post_friend'] : 0 ;
So if the checkbox is checked, variable is 1. If not, variable assigned value of 0
Checkbox is unchecked, but it loads as checked
The attribute for checked
is set to false
:
checked="false"
Any value in the checked
attribute, or even the presence of the checked
attribute, will mark the checkbox as checked. Remove the attribute and the default value of unchecked will be shown.
Example: the checked
attribute is completely removed.
<div class="form-check">
<label class="form-check-label">
<input onchange="inputCHKChange(this);"
type="checkbox"
class="form-check-input"
name="undefined[]"
required="">A</label>
</div>
Send value 0 when checkbox is not checked
Solved as below
<div class="col-xs-5 drinkingLeft">
<input type="checkbox" name="beer" id="beer"class="require-one col-xs-1" value="0"/>
<input id='beerHidden' type='hidden' value='0' name='beer'>
<label for="beer" class="col-xs-10">Beer </label>
</div>
$('#beer').on('change', function () {
this.value = this.checked ? 1 : 0;
}).change();
$("#submit").click(function () {
if(document.getElementById("beer").checked){
document.getElementById('beerHidden').disabled = true;
}
});
Send value even for unchecked checkboxes
If i understood your question correctly, the code below may help you. However i didn't understand why you are trying to use not only checkbox and hidden input with checked attribute but also oppositely.
Change HTML like this, because you don't need the hidden inputs as you are going to validate data with name and if all you want is just 1 or 0, the code in below may help you.
Updated Fiddle: http://jsfiddle.net/eaVL3/2/
HTML:
<input type="checkbox" name = "all_post" id = "all_post" value="0"> All Posts <br/>
<input type="checkbox" name = "others_post" id = "others_post" value="0"> Other's Posts <br/>
<input type="checkbox" name = "clients_post" id = "clients_post" value="0"> Cilent's Post <br/>
<input type="checkbox" name = "assigned_post" id = "assigned_post" value="0"> Task Assigned
Javascript:
$(function(){
$(':checkbox').on('change', function() {
if (this.checked == true)
$(this).val("1");
else
$(this).val("0");
});
});
Related Topics
How to Set Page Size and Set Margin in Print in Internet Explorer With JavaScript and CSS
Webcam Js Error in Chrome: Could Not Access Webcam
How to Use Owl Carousel Vertically
Onclick Table Row With Onclick Button and Bootstrap Modal Inside
How to Instantiate a JavaScript Class in Another Js File
Filter Input Text to Enter Only Number and Not Even Decimal
How to Mock the JavaScript Window Object Using Jest
Uploading Multiple Files Using Formdata()
How to Split Date and Time from the Datetime Variable in JavaScript
What Is the Purpose of the Dollar Sign in JavaScript
How to Prevent Invalid Characters from Being Typed into Input Fields
React Typescript Is Not Assignable to Parameter of Type
Reload Table After Deleting an Item from It With React
How to Show an Alert After Reloading the Page in JavaScript
Update Value in Localstorage Array by Existing Key
How to Select Json Response Item With Spaces