...

View Full Version : select list problem



bhakti_thakkar
12-23-2006, 09:30 AM
Hi all,
there is some stupid problem which i am not able to make out.i have a function to load months in a drop down. it loads properly but with a wrong seleted value.i am also passing the value which has to be selected by default but its not happening. i am not able to make it out :rolleyes:
below is my code:

<?
function DisplayMonths($field) {
print("<option value='N/A'>N/A</option>");
for ($i=1;$i<=12;$i++) {

if ($i==$field) {
$selected=' selected ';
}

switch ($i) {
case 1:
$mon="January";
break;
case 2:
$mon="February";
break;
case 3:
$mon="March";
break;
case 4:
$mon="April";
break;
case 5:
$mon="May";
break;
case 6:
$mon="June";
break;
case 7:
$mon="July";
break;
case 8:
$mon="August";
break;
case 9:
$mon="September";
break;
case 10:
$mon="October";
break;
case 11:
$mon="November";
break;
case 12:
$mon="December";
break;

} // end of switch


print("<option value=$i $selected>$mon</option>");

}

}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<?$from=1;?>
<select name="from" class="sel1"><?=DisplayMonths($from);?></select>
</BODY>
</HTML>

there is some type caste problem??? please help me.December month gets selected instead of january

Thanks in advance

GJay
12-23-2006, 09:55 AM
$selected=''; //add this
if ($i==$field) {
$selected=' selected ';
}


you're setting $selected to equal 'selected' on the first pass, for january, but then it's still equal to 'selected' the next time round the loop. So all the options are ending up being 'selected', the browser must be picking the last one.

bhakti_thakkar
12-23-2006, 10:47 AM
hi,
thanks for ur reply.it was making me mad mad mad.its finally solved.
:thumbsup:
:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum