How to Read If a Checkbox Is Checked in PHP

How to read if a checkbox is checked in PHP?

If your HTML page looks like this:

<input type="checkbox" name="test" value="value1">

After submitting the form you can check it with:

isset($_POST['test'])

or

if ($_POST['test'] == 'value1') ...

How to read if a checkbox is checked in ajax?

Toggle value for ajax using ternary operator:

document.getElementById("ch").checked ? 'y' : 'n'

function in Gravity Forms to read if a checkbox is checked?

Checkboxes have a field ID and input ID. It sounds like you want to check if no checkbox is checked? Here's an simple way to do that:

// Update "123" to your form ID.
add_action( 'gform_pre_submission_123', function( $form ) {

// The ID of your checkbox field.
$field_id = 1;

/** @var GF_Field $field */
$checkbox_field = GFAPI::get_field( $form, $field_id );
$value = $checkbox_field->get_value_submission( array() );

// Just so you can see the data that is returned. Delete this once you have.
print_r( $value );

$value = array_filter( $value );
if ( empty( $value ) ) {
// Do what you want to.
}

} );

detect unchecked checkbox php

If a checkbox is not checked it will not be posted. if(!isset($_POST['checkboxname'])) will do the trick.

Be aware, though, you should at least submit something so that you know the form was submitted in the first place.

if (isset($_POST['formWasSubmitted'])) {
//form was submitted...let's DO this.

if (!isset($_POST['checkboxname'])) {
// checkbox was not checked...do something
} else {
// checkbox was checked. Rock on!
}
}

Verifying at least one checkbox is checked php

This is too long for a comment at this point and have already outlined what you need to do in comments.

N.B.: I pulled this from a "now deleted" question as I was going to make a reference to it.

Note that if you're wanting to use the same name attribute for each checkbox, that you need to treat them as arrays using [] for all of them.

I.e.: name="the_name[]"

Base yourself on the following:

<?php

if(isset($_POST['test'])){
$a_counts = array();
foreach($_POST['test'] as $key => $val){
if(!isset($a_counts[$val])){
$a_counts[$val] = 1;
}else{
$a_counts[$val]++;
}
echo $key." => ".$val."<br>";
}
print_r($a_counts);
}
?>
<form action="" method="POST">
<input type="checkbox" name="test[]" value="red">
<input type="checkbox" name="test[]" value="green">
<input type="checkbox" name="test[]" value="yellow">
<input type="checkbox" name="test[]" value="blue">
<input type="submit" value="ok">
</form>

Or something like this which would be the better solution:

<form method="post">

<label class="heading">Select from the following:</label>
<input type="checkbox" name="check_list[]" value="Value 1"><label>Value 1</label>
<input type="checkbox" name="check_list[]" value="Value 2"><label>Value 2</label>
<input type="checkbox" name="check_list[]" value="Value 3"><label>Value 3</label>
<input type="checkbox" name="check_list[]" value="Value 4"><label>Value 4</label>
<input type="checkbox" name="check_list[]" value="Value 5"><label>Value 5</label>
<input type="submit" name="submit" Value="Submit"/>

</form>

<?php

if (isset($_POST['submit'])) {
if (!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}
}
else {
echo "<b>Please Select at least One Option.</b>";
}
}
?>

<?php

if (!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}
}
else {
echo "<b>Please Select at least One Option.</b>";
}


Related Topics



Leave a reply



Submit