Php: Possible to Automatically Get All Posted Data and Multiple Checkbox Unchecked

PHP: Possible to automatically get all POSTed data And Multiple checkbox unchecked?

Only "successful" controls are submitted. An unchecked checkbox or radio button is not "successful".

You need to declare a default value with a hidden input. Make sure the hidden input comes before the checkbox so if the checkbox is checked it will override the default hidden input since the names are the same:

<input name="p-sis-0110-1" type="hidden" value="0">
<input name="p-sis-0110-1" type="checkbox" value="1">

To use an array you need to explicitly define the indexes so they are the same:

<input name="input[0]" type="hidden" value="0">
<input name="input[0]" type="checkbox" value="1">

Get checkbox value as zero if not selected

Only checked boxes get sent to the server, there's no way to make it send another value for the unchecked boxes.

You can fill in the missing values in PHP:

$estates = [];
$estate_checkboxes = array_flip($_POST['estate-checkbox']);
for ($i = 1; $i <= $max_estates; $i++) {
if (isset($estate_checkboxes[$i])) {
$estates[] = $i;
} else {
$estates[] = 0;
}
}

$safeestatecheckbox = implode(', ', $estates);

array only storing checkboxes with value 1 (not 0)

Only successful (checked) controls are submitted. You need to add a hidden input with the default value before the checkbox. Notice you need to hard code the indexes so that the hidden default matches the checkbox value:

<input type="hidden"   name="cbox1[0]" value="0" class="chbo">
<input type="checkbox" name="cbox1[0]" value="1" class="chbo" checked="checked">
<input type="hidden" name="cbox1[1]" value="0" class="chbo">
<input type="checkbox" name="cbox1[1]" value="1" class="chbo" checked="checked">

So if not checked, only the hidden cbox1[0] with value 0 will be submitted. If checked, then cbox1[0] with value 1 will overwrite the hidden input.

You won't need the JavaScript now because you can change the checkbox to value 0 but if it is not checked it won't be submitted.

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 come checkbox state is not always passed along to PHP script?

If you want to overcome this design feature, try this:

<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />

This way, you'll always have $_POST['check_box_1'] set in the callback page, and then you can just check its value to see whether they checked it or not. The two inputs have to be in that order though, since the later one will take precedence.



Related Topics



Leave a reply



Submit