duan201444 2015-11-12 20: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 20: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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?