Forum Moderators: coopster

Message Too Old, No Replies

Checkbox variables between forms with PHP

It seems NOT easy to pass checkbox information - or am I missing something

         

NeedledNovice

4:59 pm on Jan 30, 2006 (gmt 0)



Searching novice-like through books and scouring PHP onliners, but I can't find an easy answer. All I want to do is list a batch of checkboxes on one page, and pass it to the next, where I wish to list those that were checked.
I have tried to do it using the guidance from the "... for Dummies" book, but cannot make it work. Radio boxes or text, yes. Checkboxes, no.

Last couple of hours online suggest that I am not alone. Is this seemingly easy process not so at all? Here's the bits of simple code that should work, shouldn't they?

**** From first page -
echo "<form action='TownsInThoseCounties.php' method='POST'>\n";
while ($row = mysql_fetch_array($result))
{
extract($row);
echo "<input type='checkbox' name='interest[$Area]' value='$Area'>$Area\n";
echo "<br>\n";
}
echo "<p><input type='submit' value='Select these counties'>
</form>\n";

**** Into the second -
foreach ($_POST as $field => $value)
{
echo "$field = $value<br>";
}

Birdman

9:07 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

Checkboxes won't pass a value unless they are checked. Since you only want to list the checked items, you're ok. Try something like this:

if (isset($_POST['interest']) {
foreach ($_POST['interest'] as $key => $val{
print $key .' = '.$val.'<br />';
}
}

Cheers