ewok
ewok

Reputation: 21493

html checkboxes with none selected does not POST

I am posting checkboxes from an HTML form and am having a weird problem. The first is that I have a default checked and disabled box at the top of the form, but it doesn not get included in the POST data. The second is that If I don't check something, the entire array is left out.

How can I get it to 1) include my default box and 2) POST an empty array if none are selected?

Here's the code:

<form action="file.php" method="POST">
    <label><input type="checkbox" name="options[]" value="username" checked disabled> Username</label><br>
    <label><input type="checkbox" name="options[]" value="title"> Title</label>
    <label><input type="checkbox" name="options[]" value="first"> First Name</label>
    <label><input type="checkbox" name="options[]" value="last"> Last Name</label><br>
    <label><input type="checkbox" name="options[]" value="address"> Address</label>
    <label><input type="checkbox" name="options[]" value="city"> City</label>
    <label><input type="checkbox" name="options[]" value="state"> State</label>
    <label><input type="checkbox" name="options[]" value="zip"> ZIP</label><br>
    <label><input type="checkbox" name="options[]" value="email"> Email</label>
    <label><input type="checkbox" name="options[]" value="phone"> Phone</label><br>
    <input type="submit" value="submit">
</form>

file.php

<?php var_dump($_POST)

Upvotes: 0

Views: 376

Answers (2)

Mark Miller
Mark Miller

Reputation: 7447

You could also do something like this without changing your HTML at all. Simply, create a list of all possible checkbox values and compare with those posted. As far as username is concerned, since it will always be there, you could just add it manually to the $_POST array.

// Auto insert username to $_POST array (because it's always there by default)
$_POST['options'][] = 'username';

// Create array of all possible checkbox values
$boxes = array('username','title','first','last','address','city','state','zip','email','phone');

// Compare $_POST array to list of possible checkboxes
// and create manual post array
$post_array = array();
foreach ($boxes as $box) {
    $post_array[$box] = in_array($box, $_POST['options']) ? 'checked' : 'NOT checked';
}

The output will be an array, $post_array, which will contain something like:

Array
(
    [username] => checked
    [title] => checked
    [first] => NOT checked
    [last] => NOT checked
    [address] => checked
    [city] => checked
    [state] => NOT checked
    [zip] => NOT checked
    [email] => NOT checked
    [phone] => checked
)

Upvotes: 1

Machavity
Machavity

Reputation: 31654

This is a part of standard HTML (i.e. not a browser thing). By definition, unchecked boxes are never successful. Consider a different data structure, or adding something like

if(isset($_POST['options'])) { 
     //work with options here
}

If that won't work, you can always include a hidden element, that will at least get you the value in $_POST

<input type="hidden" name="options[]" value="NA">

Upvotes: 3

Related Questions