How get value for unchecked checkbox in checkbox elements when form posted?
First way - hidden fields (disadvantage: the user can manipulate the value of the field (but one can manipulate the value of the checkbox too, so it's not really a problem, if you only expect 1 or 0))
<form action="" method="post">
<input type="hidden" name="status_1" value="0" />
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="hidden" name="status_2" value="0" />
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="hidden" name="status_3" value="0" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" />
</form>
<?php
var_dump($_POST);
/*
* checking only the second box outputs:
*
* array (size=3)
'status_1' => string '0' (length=1)
'status_2' => string '1' (length=1)
'status_3' => string '0' (length=1)
*/
Second way - to assign default value for non-set indexes:
<form action="" method="post">
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" />
</form>
<?php
for($i = 1; $i<=count($_POST); $i++) {
$_POST["status_$i"] = isset($_POST["status_$i"]) ? $_POST["status_$i"] : 0;
}
var_dump($_POST);
/**
* Here we will be checking only the third checkbox:
*
* array (size=3)
'status_3' => string '1' (length=1)
'status_1' => int 0
'status_2' => int 0
*/
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>
Get values of an unchecked and checked checkboxes in PHP
Thanks to people who helped me in the comment section of my question.
Here's my final version of my code.
So I'm doing the usual while loop
to retrieve data using mysql
. Well don't get mad at me I was assigned to years old of web application for certain company.
Anyhow, I used @Bad_boy's logic to solve this issue.
The old code that I had is,
Note: $ctr
is the increment thing-y per row.
echo '
<td>
<input type="checkbox" name="doctor_'. $ctr .'[]" class="day_cbox" value="1">
</td>
';
And tada! I'm not good at explaning how it works but yeah, it worked! Hahahaha! Thanks!
while ($result = mysql_fetch_array($row)) {
echo '
<tr class="row-doctor">
<td align="center">'. ucwords(strtolower($result['doctor_name'])) .'</td>
';
for ($i = 1; $i <= 25; $i++) {
echo '
<td>
<input type="hidden" name="doctor_'. $ctr .'['. $i .']" value="0">
<input type="checkbox" name="doctor_'. $ctr .'['. $i .']" class="day_cbox" value="1">
</td>
';
}
echo '
<td align="center" class="blue strong"><span class="frequency_'. $ctr .'">0</span></td>
</tr>
';
$ctr++;
}
How to get values from unchecked checkbox
As @EhsanT said i have solved the problem with sql command...Thankyou EhsanT....
if(isset($_POST["submit"]))
{
$ans=array();
$ans=$_POST['student'];
$re=mysql_query("SELECT `st_id` FROM `student_info`
WHERE `sem` ='$selsem'
AND `st_id` NOT IN (".implode(',',array_map('intval',$ans)).")");
while ($ro = mysql_fetch_row($re)){
mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$ro[0]','$seldate','$selsem','$selperiod','$selsub','0')"); }
//Here goes array
for($i=0;$i<count($_POST['student']);$i++)
{
$id=$_POST['student'][$i];
//echo $id;
$check=1;
mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$id','$seldate','$selsem','$selperiod','$selsub','$check')");
//print_r ($_POST['student']);
}
}
How to submit unchecked checkboxes to controller
You could add a hidden field with the same name before every checkbox you want to receive, like :
<input type="hidden" name="checkbox-1" value="0" />
<input type="checkbox" name="checkbox-1" value="1" /> My checbox 1
That will send the hidden field with the 0 value when the field is unchecked and send the proper truthy value when it's checked.
NOTE: Just make sure you're adding the hidden field first so you'll receive just the checked one when the field is checked.
How to get value of checkbox on check or uncheck without submit form
First you will need to set an event listener to listen for the change event and trigger a function.
If you give your elements id
's you can make things easier to target that element.
You will also want to place your javascript into <script>
tags. Some people place them in the body of the page or at the very bottom. Me personally, I like to place my in the <head>
tag as using window.onload
will only attempt to getElementById()
when the page has loaded.
window.onload=function(){ // Trigger when the page is ready
//Set change event
document.getElementById('Confirmation').addEventListener('change', Confirm, false);
}
function Confirm(){
var Confirmation=document.getElementById('Confirmation').checked;
var Message;
if(Confirmation){
Message="Confirmed";
}else{
Message="Not Confirmed"
}
document.getElementById('Result').innerHTML=Message;
}
<input type="checkbox" id="Confirmation" />
<span id="Result">Not Confirmed</span>
Related Topics
Regex Backreference to Match Different Values
How to Call a PHP Script/Function on a HTML Button Click
Preg_Match(); - Unknown Modifier '+'
What Can Use for Datetime::Diff() for PHP 5.2
Read a File Backwards Line by Line Using Fseek
What Is Pdo & Why Should I Use It
How to Select Randomly with Doctrine
Parse Xml Namespaces with PHP Simplexml
How to Send Emails via Cron Job Usng PHP MySQL
Use Strings to Access (Potentially Large) Multidimensional Arrays
Using PHP to Populate a <Select></Select> Dropdown
How to Create Codeigniter Batch Insert Array
Warning: File_Get_Contents(): Https:// Wrapper Is Disabled in the Server Configuration by All