Jump to content

help getting foreach() to populate checkboxes


glennn.php

Recommended Posts

 

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

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

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>

 

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.