duan201444 2015-11-12 12:34
浏览 44
已采纳

从动态下拉列表php mysql中打印选定的值

I have the below code where I can get dynamic value into drop down list from mysql db but i can't print selected value when i click on submit button. can anyone help me urgntly ?

<?php
include("includes/config.inc.php");
$query = "SELECT * FROM category";
$result = mysql_query ($query);
echo "<select class='turnintodropdown'  name='CategoryID' ><option value=''>All</option>";
while($r = mysql_fetch_array($result)) {
    echo "<option value=".$r['CategoryID'].">".$r['CategoryName']."</option>"; 
}
echo "</select>";
if (isset($_POST['submit'])) {
    $selected_val = $_POST['CategoryID'];  // Storing Selected Value In Variable
    echo "You have selected :" .$selected_val;  // Displaying Selected Value
}
?>  
<input type="submit" name="submit" value="Get Selected Values" />
</form>
  • 写回答

2条回答 默认 最新

  • doubu4826 2015-11-12 12:40
    关注

    If you want to preselect the selected value of the then you can use the following code. Also check your form method attrivute to see if it set to post (note that mysql_* functions are deprecated, it's better to use PDO using prepared statements).

    while($r = mysql_fetch_array($result)) {
        if (!empty($_POST['CategoryID']) && $_POST['CategoryID'] == $r['CategoryID']) {
            $selected = 'selected="selected"';
        } else {
            $selected = '';
        }
        echo "<option ".$selected." value=".$r['CategoryID'].">".$r['CategoryName']."</option>"; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部