Forum Moderators: coopster
$month =
array(01=>array('January'),02=>array('February'),03=>array('March'), 04=>array('April'),05=>array('May'),06=>array('June'),07=>array('July'), 08=>array('August'),09=>array('September'),10=>array('October'), 11=>array('November'),12=>array('December'));
1)I want to loop throught the array and populate a dropdownlist in a html form with the above array. I want the string version of the month to be a diplay text, and number to be in the value. like this
<select>
<option value="01">January</option>
</select>
2)when the form is submitted, I want to echo out the selected value.
any help would be much apperciated.
thanks.
[edited by: eelixduppy at 12:04 am (utc) on Oct. 6, 2008]
[edit reason] side-scroll [/edit]
You'll need to give the select a name, then if you're using POST method on the form it will be $_POST['whatevername'] or if GET, $_GET['whatevername'].
Out of curiosity, why do you have the month names as single-element arrays?
I recommend reforming your 2-dimensional array to either this format: array("January"=>1, "February"=>2), or using a regular array like such: array("January","February","March")
<select name="months">
<?
// for associative array
foreach($months as $k=>$v){
?><option value="<?=$v?>">$k</option><?
}// for regular array
$monNum = count($months);
for($i=0; $i<$monNum; $i++){
?><option value="<?=$i?>"><?=$months[$i]?></option><?
}
?>
</select>
Now to echo out the string value of the month after using the $_POST array would be:
// validate POST variables
// associative array method using the date function
echo date("F", mktime(0,0,0,$_POST['month'],1,2000));
// normal array method
echo $months[$_POST['month']];
Hope this helps ...
<select name="months">
<?
$monNum = count($months);
for($i=0; $i<$monNum; $i++){
if($i == $_POST['months']) $selected = "selected='selected'";
else $selected = "";
?><option value="<?=$i?>" $selected><?=$months[$i]?></option><?
}
?>
</select>
Something to that effect would work. May need to be tweaked a little.
Also note: depending on your PHP error and warning levels, you may need to validate if the $_POST['months'] even exists before going into that if statement, or it may throw a warning at you.