jkuboschek Posted October 12, 2007 Share Posted October 12, 2007 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 More sharing options...
Barand Posted October 12, 2007 Share Posted October 12, 2007 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> Link to comment https://forums.phpfreaks.com/topic/72894-solved-php-checkboxes-dont-wanna/#findComment-367656 Share on other sites More sharing options...
jkuboschek Posted October 12, 2007 Author Share Posted October 12, 2007 Thanks for the reply. Is there any way to have checked/unchecked values? Link to comment https://forums.phpfreaks.com/topic/72894-solved-php-checkboxes-dont-wanna/#findComment-367657 Share on other sites More sharing options...
Barand Posted October 12, 2007 Share Posted October 12, 2007 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> Link to comment https://forums.phpfreaks.com/topic/72894-solved-php-checkboxes-dont-wanna/#findComment-367658 Share on other sites More sharing options...
jkuboschek Posted October 12, 2007 Author Share Posted October 12, 2007 Thanks, it's working. The error was here: name='check[$i]' instead of name='check[]' Link to comment https://forums.phpfreaks.com/topic/72894-solved-php-checkboxes-dont-wanna/#findComment-367966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.