Forum Moderators: coopster
Html page with static drop down boxes go to a dynamically created page pulled from a db. Need to redisplay the form with the previously selected dropdown displayed as selected.
One trick I've used frequently, is to display it like this:
HTML FORM:
<form action=mypage.php>
<select name=selbox size=1>
<option value=>Select one</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>
user selects 3:
PHP FORM:
<form action=mypage.php>
<select name=selbox size=1>
<option value=$selbox SELECTED>$selbox</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>
which results in a dropdown box looking like:
<form action=mypage.php>
<select name=selbox size=1>
<option value=3 SELECTED>3</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>
The upside is, the value of 3 gets displayed. Downside is that 3 is repeated twice and you risk confusing the user.
While this works, I'd prefer to have something like
<form action=mypage.php>
<select name=selbox size=1>
<option value=1 $1selected>1</option>
<option value=2 $2selected>2</option>
<option value=3 $3selected>3</option>
<option value=4 $4selected>4</option>
</select>
</form>
But with 50 choices in 10 dropdowns, that is a lot of typing. Not the programmers way.
I've solved this problem in other ways for other apps with some complicated code. What I'm wondering, has anyone solved the issue in an elegant, simple, quick and dirty way?
<form action=mypage.php>
<select name=selbox size=1>
<option value=1 $1selected>1</option>
<option value=2 $2selected>2</option>
<option value=3 $3selected>3</option>
<option value=4 $4selected>4</option>
</select>
</form>
do
<form action=mypage.php>
<select name=selbox size=1>
<option $selectedbox[1] value=1>1</option>
<option $selectedbox[2] value=2>2</option>
<option $selectedbox[3] value=3>3</option>
<option $selectedbox[4] value=4>4</option>
</select>
</form>
Then in the php page
$selectedbox[$selbox]="SELECTED";
That should do the trick.
<form action="mypage.php">
<select name="selbox" size="1">
<option value="">Select one</option>
<? foreach(array(1,2,3,4) AS $value) {?>
<option value="<?=$value?>"<?=($selbox==$value)?' selected="selected"':''?>><?=$value?></option>
<? }?>
</select>
</form> and that's it.
array() [php.net] is not a function, it's a language construct to create an array.
the other thingy is not another form of if, it's an expression [php.net]:
There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator:<?php
$first? $second : $third
?>If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.
<?=$value?> is the same as writing <? echo $value;?>
[php.net...]
echo() also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. This short syntax only works with the short_open_tag configuration setting enabled.