开发者

php generated option selected by default

I'm probably blind to my own mistake again, but why doesn't 2011 becom开发者_高级运维e the default selected year? (The -- are for debugging, they show up that the if statement works correctly.)

for($year=1900;$year<=2050;$year++) {
 if ($year==date("Y"))
   echo "<option value='".$year."' selected='selected'>--".$year."</option>";
 else
   echo "<option value='".$year."'>".$year."</option>";
}

The selected='selected' should just work. (link) So it is either a stupid mistake(i really don't see one) or some effect from php..

UPDATE: found the problem.. the ftp didn't overwrite the file properly. Now it works. Thanks for the fast help, when these things happen I always start doubting my own sanity. (cant send this as answer because i have a low rep)


PHP has no effect on the browser behaviour, so if it doesn't work, it is a stupid mistake in the HTML output. ;)

Best thing is checking if the selected variant is actually outputted. Could be that the comparison evaluates to false, so it's always the second line. A little debugging should point out fast what is wrong. Check the output in the browser or var_dump the values of $year and date('Y') to see if they return what you expect.

At first sight I see no error here. Do you have any Javascript that might influence the selection?


CTRL + SHIFT + R if you use firefox. I had that problem very often too .. It should be just the cache.

Btw.: You should rather use double quotes for html attributes. That's more "usual". :)


Are you checking the generated HTML? Maybe this code works well (it seems it does), but the problem is in select, or in another place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜