php下拉选择错误

时间:2013-07-23 15:32:34

标签: php forms select drop-down-menu

我有以下代码,它完美地将必要的数据插入数据库。但问题是,当我想实现select =“select”..所以,如果客户返回,他将看到他从下拉菜单中选择的内容......我怎么能这样做,任何想法......

<pre>
<?php
//array generated from mysql to feed first dropdown menu.
$newOptions = array();
    foreach ($input as $option) {
        $wclID = $option['desc'];
        $nameF = $option['fame'];
        $nameL = $option['lname'];
        $id = $option['ID'];
        $newOptions[$wclID][$id] = $nameL." ".$nameF;
    }

//second array for second dropdown menu
$array = array('ent','res');    
?>


   <div class="control-group">
        <div class="controls">
            <form  id="checkEnt" name="che" method="post" action="com.php?ID=<?=$ID?>">
                <input type="hidden" value="che" name="ent">
                <table class="table table-striped span10">
                    <tbody>
                        <? foreach ($newOptions as $wclID => $list) {  ?>
                            <tr><td width="5%">
                                    <h5><?=$wclID?> AA</h5>
                                </td>
                                <td width="10%">
                                    <label class="control-label" for="inputWei"><?=_('Boy')?></label>  
                                    <select class="input-xlarge" id="input" name="drop[0][]">   
                                        <option value=""><?=_('[select]')?></option>
                                        <?php 
                                            foreach ($list as $key => $value) {
                                            ?><option value="<?=$key?>"><?=$value?></option>

                                            <?php    } ?>
                                    </select>
                                </td>
                                <td width="10%">
                                    <label class="control-label" for="inputWei"><?=_('Res')?></label>
                                    <select class="input-xlarge" id="drop" name="drop[1][]">
                                        <option value=""><?=_('[select]')?></option>
                                        <?php
                                         foreach ($array as $key => $value) {
                                            ?><option value="<?=$value?>"><?=$value?></option>

                                            <?php } ?>
                                    </select>
                                </td>
                            </tr>

                              <?
                            }
                        ?> 

                            <tr><td colspan="3">
                                    <div class="modal-footer">
                                        <button type=reset class="btn btn-danger"><?=_('Reset')?></button>
                                        <button class="btn btn-primary" ID="btnSave"><?=_('Save')?></button>
                                    </div>
                                </td>
                            </tr> 


                    </tbody>
                </table> 
            </form>   
        </div>
    </div>
</pre>

2 个答案:

答案 0 :(得分:2)

我最近做了类似的事情。这是用于从数据库返回的数据是否正确?如果是这样,那么我会实现类似的东西。

在选项标签上,我会尝试使用if语句来查看数据库中的内容,然后是否调用selected = selected

<option value="Something" <? if(in_array('Something',$nameofArray)) echo 'selected="selected"';?>>Something</option>

或者您可能想尝试

<?foreach ($list as $key => $value) {?>
    <option value="$value" <? if($value) echo 'selected="selected"';?>>$value</option>

 <?}?>

答案 1 :(得分:-1)

使用&#34;选择&#34;选项标签的属性:

http://www.w3schools.com/tags/att_option_selected.asp