glennn.php Posted September 10, 2008 Share Posted September 10, 2008 i'm having trouble getting this array to populate the selected checkboxes (this isn't the code i've used in the form fields, i'm just trying to make it clear what i'm trying to do). $offering = $_POST['highest_deg']; foreach($offering as $x) { $x... ; } <input type="checkbox" name="highest_deg[]" value=03 (if $x = '03', CHECKED), etc ... /> <input type="checkbox" name="highest_deg[]" value=04 /> <input type="checkbox" name="highest_deg[]" value=05 /> could someone kindly show me how to CHECK a group of checkboxes from the array i have...? i appreciate it much. GN Link to comment https://forums.phpfreaks.com/topic/123607-help-getting-foreach-to-populate-checkboxes/ Share on other sites More sharing options...
glennn.php Posted September 10, 2008 Author Share Posted September 10, 2008 this almost worked: <input type="checkbox" name="highest_deg[]" value="03"<?php echo ($x == 03 ? " CHECKED" : ""); ?> <input type="checkbox" name="highest_deg[]" value="04"<?php echo ($x == 04 ? " CHECKED" : ""); ?> <input type="checkbox" name="highest_deg[]" value="05"<?php echo ($x == 05 ? " CHECKED" : ""); ?> but of course it only selects the last value in the array - i guess i need to do some kind of count...? help? (i've been searching for the right tutorial for a couple of hours with no success)... Thanks Link to comment https://forums.phpfreaks.com/topic/123607-help-getting-foreach-to-populate-checkboxes/#findComment-638337 Share on other sites More sharing options...
ainoy31 Posted September 10, 2008 Share Posted September 10, 2008 See if this is what you are looking for: <? error_reporting(E_ALL ^ E_WARNING); $arrayDeg[] = ""; if (array_key_exists('highest_deg', $_REQUEST)) { foreach($_REQUEST['highest_deg'] as $data) { $arrayDeg[] = $data; } //print_r($arrayDeg); } ?> <form name = "test" action="test.php" method="post"> <? for($x=3; $x<10; $x++) { echo $x;?>: <input type="checkbox" name="highest_deg[]" value="<?=$x;?>" <?if(array_search($x, $arrayDeg) !== FALSE){?>checked<?}?>/> <? echo "<br>"; } ?> <input type="submit" name="submit" value="Submit"> <input type="reset" value="Reset" /> </form> Link to comment https://forums.phpfreaks.com/topic/123607-help-getting-foreach-to-populate-checkboxes/#findComment-638481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.