Forum Moderators: coopster

Message Too Old, No Replies

Strange problem with checkboxes

         

optik

5:58 pm on Oct 13, 2009 (gmt 0)

10+ Year Member



This is a condensed version of a page to demo the problem, the form sends to the page it is on.

<?

$count=0;

foreach (array_keys($_POST) as $key) {
$$key = $_POST[$key];
$count++;

echo ${'check_'.$count};

}

?>

<form action="form_to_self.html" method="post">
<input name="check_1" type="checkbox" value="true" />
<input name="test" type="checkbox" value="true" />
<input name="check_2" type="checkbox" value="true" />

<input name="submit" type="submit" />

</form>

For some reason if check boxes 1 and 3 are checked php echos truetrue as expected but if all three are checked it only echos true instead of truetrue.

TheMadScientist

10:18 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's because your $count is off...

On the first loop you are echoing check_1 on the second loop ($count) you are trying to echo check_2 (it's not set yet) on the third loop you are trying to echo count_3 (it doesn't exist). You'll probably need to wrap your counter in a conditional... if(strpos($_POST['key'],'check_')) { $count++; } or something to that effect.