Jump to content

[SOLVED] PHP - checkboxes don't wanna....


jkuboschek

Recommended Posts

Hey folks, I've got a problem with checkboxes and I just can't figure out why the values are all whack.

 

Here's what I've got:

 

Page 1 - project listings including checkboxes with each listing:

while($projects = mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td><input type=\"checkbox\" name=\"check[]\" value=\"on\">".($i+1)." <a href=\"".$projects['project_url']."\" target=\"blank\">".$projects['project_name']."</a></td>";
// .........
}

As you can see, $check is an array and is supposed to look like this:

$check[0]: on|NULL

$check[1]: on|NULL

etc. depending on whether the box was checked or not

 

$check will be passed on to page 2 as POST parameter.

 

Now, next page:

for ($i=0;$i<=sizeof($id);$i++)
{
	echo $id[$i]." ".$price[$i]." ".$time[$i]." ".$check[$i]."<br />";
//....
}

Alright, here's what happens: Let's assume I check 3 random boxes on page 1 (not the first three). On page two, it then tells me that the first three boxes have been checked. Somehow the $check[] array is filled incorrectly or blank entries are disposed of. Ideas?

Link to comment
https://forums.phpfreaks.com/topic/72894-solved-php-checkboxes-dont-wanna/
Share on other sites

Only checked c/boxes are posted.

 

<?php
if (isset($_POST['check']))
{
    echo "You chose ";
    foreach ($_POST['check'] as $v) echo "$v ";
}

?>
<hr>
<form method='post'>
<input type="checkbox" name="check[]" value="1"> 1<br>
<input type="checkbox" name="check[]" value="2"> 2<br>
<input type="checkbox" name="check[]" value="3"> 3<br>
<input type="submit" name="sub" value="Submit">
</form>

You can also specify the check array indexes

<?php
if (isset($_POST['check']))
{
    echo "You chose <br>";
    for ($i=1; $i<=3; $i++)
    {
        echo $i , ' - ', (isset($_POST['check'][$i]) ) ? 'checked' : 'unchecked', '<br>';
    }
}



?>
<hr>
<form method='post'>
    <?php
    for ($i=1; $i<=3; $i++)
    {
    echo "<input type='checkbox' name='check[$i]' value='ON'> $i<br>";
    }
    ?>
<input type="submit" name="sub" value="Submit">
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.