html - Select option default based on database variable -


i have form, simple select box, , have text field pulls value. want use value in $defaultselection default value select options. if $defaultselection = 4 default selected option d.

using php/html

`$defaultselection`  

contains integer between 1-4.

<select >     <option value="1">a</option>     <option value="2">b</option>     <option value="3">c</option>     <option value="4">d</option> </select>  

that should it:

<select >     <option value="1" <?php echo ($defaultselection == 1)?"selected":""; ?>>a</option>     <option value="2" <?php echo ($defaultselection == 2)?"selected":""; ?>>b</option>     <option value="3" <?php echo ($defaultselection == 3)?"selected":""; ?>>c</option>     <option value="4" <?php echo ($defaultselection == 4)?"selected":""; ?>>d</option> </select>  

or other one:

<select >     <option value="1" <?php if ($defaultselection == 1) echo "selected"; ?>>a</option>     <option value="2" <?php if ($defaultselection == 2) echo "selected"; ?>>b</option>     <option value="3" <?php if ($defaultselection == 3) echo "selected"; ?>>c</option>     <option value="4" <?php if ($defaultselection == 4) echo "selected"; ?>>d</option> </select>  

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -