How to Get All Checkbox Variables Even If Not Checked from HTML to PHP

how do I get all checkbox variables even if not checked from HTML to PHP?

I just ran into this problem myself. I solved it by adding a duplicate hidden field with the same name. When the browser sends this information, the second field overrides the first (so ensure that the hidden field comes first).

<input type="hidden" name="foo" value="">
<input type="checkbox" name="foo" value="bar">

If the checkbox is not checked you get:

$_REQUEST[ 'foo' ] == ""

If the checkbox is checked you get:

$_REQUEST[ 'foo' ] == "bar"

How to get all uncheck checkboxes

You would need an array of all checkbox names you have on your page and then you could loop through this array counting how many of the checkboxes are not a key in the array $_POST.
Another solution would be to remove the names from the checkboxes and add them to hidden input which are updated by JS if the checkboxes change.

jQuery("input[type='checkbox']").change(function(){
jQuery(this).parent().find("input[type='hidden']").val(jQuery(this).prop("checked"));
}

This code assumes you have a container for each checkbox containing one checkbox and one hidden input.

Post checkbox values when checkbox isn't checked

The short answer is the browser doesn't send unchecked checkboxes. But one possible work around is to on the php side set a defaults array

So for a form that included fields like

<input type='checkbox' name='checkbox[a]' value='1'/>
<input type='checkbox' name='checkbox[b]' value='1'/>
<input type='checkbox' name='checkbox[c]' value='1'/>

you would say

$checkbox_defaults = array(
"a" => 0,
"b" => 0,
"c" => 0
);

Then on PHP say

$_POST["checkbox"] = array_merge($checkbox_defaults, $_POST["checkbox"]);

NOTE this only works for string indexed arrays ... if you need to work with numerically indexed arrays the php should look like this.

$checkbox_defaults = array(
0 => 0,
1 => 0,
2 => 0
);
foreach($checkbox_defaults as $k=>$v){
$_POST["checkbox"][$k] = (isset($_POST["checkbox"][$k])?
$_POST["checkbox"][$k]:$v);
}

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++;
}

HTML checkbox value submitting despite not being checked (PHP)

The problem is in your jQuery .ajax() handler. In a "natural" form submit, the checkbox would not be posted, but you are doing the post yourself via ajax after having collected the form data yourself.

In this:

// variables for data
$(this).find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();

// load loaded variables into array
form_data[name] = value;
});

You are selecting everything that has a ['name'] and assigning its value on form_data[]
The checkbox does have both a name and a value, so you are putting the name:value pair in form_data whether or not the box is checked.

Immediately after, you do:

    $.ajax({
type: 'post',
url: ajaxURL,
data: form_data,
...(etc)

where you are sending the data: form_data as the post body, which includes the checkbox "phoneConsent:1" pair.

To avoid this, you'll have to process the controls a bit smarter, check the input control type and send checkbox (or radiobutton) data only when the control is checked.



Related Topics



Leave a reply



Submit