0

ok I've been looking all over the net and I cannot find anything about this issue anywhere, other than the same problem is mentioned once in a great while, but never shown how to rectify this.

I'm at a point where I can't do anything else and I could use some insight as to how to fix this problem.

php will not store false values in a checkbox array.
say, for instance, you have a table:

<table>
<tr>
<td><input type="checkbox" name="box[]" value="box1"/></td>
<td><input type="checkbox" name="box[]" value="box2"/></td>
<td><input type="checkbox" name="box[]" value="box3"/></td>
<td><input type="checkbox" name="box[]" value="box4"/></td>
<td><input type="checkbox" name="box[]" value="box5"/></td>
</tr>
</table>

The problem that I am consistently running into is that say boxes 2 and 4 are checked. Since php flat out refuses to store false values in an array, that means:

box[0]=box1, box[1]=box2

this throws off the true array, because it should be:
box[0]=false, box[1]=box2, box[2]=false, box[3]=box4, box[4]=false

or something to that effect.

this is particularly frustrating because I'm using an associative array that lines the user's ID number with several arrays within this array. If the values cannot store false, then different users end up with different qualities being checked where they shouldn't be, because of the fact that php will not store false values in a checkbox array, or radio button array, for that matter.

so how does one get around this problem?
I've tried isset but it doesn't really work, either, because php just ignores the false values, so an array that should contain 5 elements only contains (for this example) 2 elements, both of which will come back as true since they hold a value, tho the value is in the wrong place.

I'm getting kind of desperate here, and I don't know any other way around this, other than something that's really, really impractical. Any help would be greatly appreciated.

thanx

Edited by sleign: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by sleign
0

The following should do the trick the the result being in the $res array

<?php
$var=$_POST['box'];
$i=1;
$res=array();
$var=(empty($var))?array():$var;
foreach ($var AS $val) {
    $n=substr($val,-1);
    while ($i<$n) {
        $res[]=false;
        $i++;
        }
    $res[]=$val;
    $i++;
    }
while ($i<=5) {
    $res[]=false;
    $i++;
    }

?><form method="POST">
<table>
<tr>
<td><input type="checkbox" name="box[]" value="box1"/></td>
<td><input type="checkbox" name="box[]" value="box2"/></td>
<td><input type="checkbox" name="box[]" value="box3"/></td>
<td><input type="checkbox" name="box[]" value="box4"/></td>
<td><input type="checkbox" name="box[]" value="box5"/></td>
</tr>
</table>
<input type="submit" value="Send">
</form>

Edited by cwarn23: php opening tag

0

that works beautifully.
I altered it a bit so that it'd work with my associative array and used the ID number of the person for each of the checkbox values in the same row as the user.

when I format it to spit out the IDs with the checkboxes that were selected, they line up.

thank you very, very much :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.