简体   繁体   English

试图在下拉列表php中显示先前选择的值

[英]trying to display previously selected value in dropdown list php

I have made some code to create a dropdown in a webpage and you can select among 2 currency values namely USD and SGD. 我编写了一些代码以在网页中创建下拉菜单,您可以在2个货币值中进行选择,即USD和SGD。 I have been able to get the value for the currency field while entering the data in the database. 在数据库中输入数据时,我已经能够获取货币字段的值。 But while trying to edit the entry in the database, I am able to use $_POST to get all entries to display except the value of currency. 但是,在尝试编辑数据库中的条目时,我可以使用$ _POST来显示所有显示的条目(货币值除外)。 I would ideally want to display the previously selected value on the dropdown. 理想情况下,我希望在下拉列表中显示先前选择的值。 As of now the drop down on the edit page just displays the default "Please Choose" and doesn't show the previously selected value. 到目前为止,编辑页面上的下拉列表仅显示默认的“请选择”,并且不显示先前选择的值。 any help would be greatly appreciated. 任何帮助将不胜感激。

Code : 代码:

  <select id="currency" name="currency" placehoder="Currency"> <option value='' disabled selected style='display:none;'>Please Choose</option> <option value="SGD">SGD</option> <option value="USD">USD</option> </select> 

And I am trying to somehow display the previously read value that exists in the database and display that instead of the "Please Choose" so that while editing I don't have to re-select the currency value. 而且,我试图以某种方式显示数据库中存在的先前读取的值,并显示该值而不是“请选择”,以便在编辑时不必重新选择货币值。

Supposing you stored in $currency the value from DB: 假设您将$currency的值存储在DB中:

<select id="currency" name="currency" placehoder="Currency">
    <option value='' disabled style='display:none;'>Please Choose</option>
    <option value="SGD"<?php echo $currency == "SGD" ? " selected" : ""; ?>>SGD</option>
    <option value="USD"<?php echo $currency == "USD" ? " selected" : ""; ?>>USD</option>
</select>

Use PHP to Solve it.. 使用PHP来解决。

<select name="select_limitby" onChange="frm_sub()">
            <?php if($_SESSION[select_limitby]!='') { ?>
                <option value="<?php echo $_SESSION[select_limitby]; ?>" <?php if($_POST[select_limitby]=='$_SESSION[select_limitby]') {?> selected="selected" <?php }?>><?php echo $_SESSION[select_limitby]; ?></option>
            <?php } ?>
            <option value="">Default</option>
            <option value="9" <?php if($_POST[select_limitby]=='9') {?> selected="selected" <?php }?>>9</option>
            <option value="12" <?php if($_POST[select_limitby]=='12') {?> selected="selected" <?php }?>>12</option>
            <option value="15" <?php if($_POST[select_limitby]=='15') {?> selected="selected" <?php }?> >15</option>
        </select>

USE session if it does not work... otherwise you can leave session... 如果无法使用,请使用会话...否则,您可以离开会话...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM