开发者

Auto select value of dropdown

How can I auto select a field in dropdown.

Say if someone goes to www.xyx/form/?abc Some value gets selec开发者_StackOverflow社区ted in the dropdown,

Or if someone goes to www.xyx/form/?def Some other value gets selected in the dropdown.

I am comfortable with JS and php.


assuming example.com/?sel=xxx

<?php
  $sel = $_GET['sel'];

?>
<select ...>
  <option val="xxx" <?php if($sel==='xxx') echo 'selected="selected"';?>>Option XXX</option>
  <option val="yyy" <?php if($sel==='yyy') echo 'selected="selected"';?>>Option YYY</option>
</select>

No Javascript needed.


PHP

<select name="select">
 <option value="abc"<?php ($_GET['select'] == 'abc'? echo 'selected="selected"' : ''); ?>>ABC</option>
 <option value="def"<?php ($_GET['select'] == 'def'? echo 'selected="selected"' : ''); ?>>DEF</option>
</select>


<option value="abc" <?php echo isset($_GET['abc']) ? 'selected="selected"' : ''; ?>>abc</option>


hmmm, so what will you do when you have 100s of items in the Options list? The other ideas wont look so great then.

Then you will need to just simply write 1 line of code at the end of select tag:

<?php if(isset($_POST['env_foil_color'])) echo "<script>document.getElementById('env_foil_color').value='{$_POST['env_foil_color']}';</script>"; ?>

where, 'env_foil_color' is the select tag's ID and Name both

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜