下拉列表选中的项目不打印

时间:2012-08-08 01:07:09

标签: php mysql html

我正在尝试打印下拉列表选中的项目。我已经很好地显示了dropdwon列表菜单。但是当我选择一个选项时,它不会打印选项。我曾尝试过很多方面。但还没到!请帮帮我,这是我的以下代码。

<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php 
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>

<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
    echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>

<input type="submit" name="submit" value="save category" />
</form>

<?php
if($_GET){
   echo 'The year selected is'.$_GET['category'];
}
?>

2 个答案:

答案 0 :(得分:0)

$_GET['category']

应该是

$_POST['category']

javascript示例:

<html>
 <head>
  <script type="text/javascript">
    window.onload = function() {
        var eSelect = document.getElementById('cat');
        eSelect.onchange = function() {

      document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
      document.getElementById("displaytext").style.display= 'block';

        }
    }
  </script>
 </head>
 <body>
    <select id="cat" name="cat">
        <option value="x">X</option>
        <option value="y">Y</option>
        <option value="other">Other</option>
    </select>
    <div id="displaytext" style="display: none;" ></div>

</body>
</html>

答案 1 :(得分:0)

您的代码中存在问题,请尝试使用此代码:

<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php 
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>

<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
    echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>

<input type="submit" name="submit" value="save category" />
</form>

<?php
if($_GET){
   echo 'The year selected is'.$_GET['category'];
}
?>