View Full Version : Option box & dynamic 'Selected' question

03-13-2008, 02:58 AM

Im having a little bit of fun trying to get my head around selected, ive used it dozens of times and its been fine, but upon closer inspection im just wondering if the issues im seeing are the norm or if i could be writing things poorly.

heres some basic code:

$rnd = rand(1, 6); // Minimum value , Maximum Value
<form name="form1" method="post" action="">
<select name="select">
<option value="1" <? if ($rnd == '1') {echo "selected";} ?> >1</option>
<option value="2" <? if ($rnd == '2') {echo "selected";} ?> >2</option>
<option value="3" <? if ($rnd == '3') {echo "selected";} ?> >3</option>
<option value="4" <? if ($rnd == '4') {echo "selected";} ?> >4</option>
<option value="5" <? if ($rnd == '5') {echo "selected";} ?> >5</option>
<option value="6" <? if ($rnd == '6') {echo "selected";} ?> >6</option>

</form><? echo "$rnd"; ?>

ok, so im randomly generating a number 1-6, and then asking for that random number to be automatically selected in the drop-down box.
Im also echoing the value, for clarity.

What im finding is that if i load the page up its correct, however if i refresh the page (F5 or the refresh browser button) the selected number isnt what is being echoed. It only works when calling the URL, hitting the [>] button.

I cant for the life of me understand why this is the case, its still going through the entire script again, so why isnt it functioning in the expected manner?

It works in part, but not entirely, is it something i have to accept or is there a solution?