duanfei8897 2011-05-09 16:27
浏览 61
已采纳

php如何从下拉列表中回显名称?

in another words, i have an drop-down list:

<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />

what i want to do is to echo out Backgammon or Chess based on their values and on witch one is selected. here is what i have so far. but i get numbers instead of names

$values = $_POST['gamelist'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['submit']) && ($_POST['submit'] == 'Submit')) {
echo $values;
}
}

thanks

  • 写回答

3条回答 默认 最新

  • duandi1636 2011-05-09 16:34
    关注

    In $_POST['gamelist'] you should have '1' or '2'.

    To display it, you should use something similar to the following:

    $options = array(
        '1' => 'Backgammon',
        '2' => 'Chess',
    );
    
    $value = $_POST['gamelist'];
    
    echo $options[$value]; // will echo proper option, assuming NO multiselect
    

    This should definitely work.

    FYI: The part contained within the <option> tag is not being passed with form data, only the values assigned within value attributes to chosen options.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部