Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.