duandeng2265 2017-02-23 02:11
浏览 51
已采纳

来自数据库的onchange下拉列表 - php,没有java或ajax

I have a problem, I want to display all rows from database to dynamic onchange select form, but it displays only first or only last row. Could you please help me someone?

this is my code:

<?php
   $selected11 = '';
   function get_options11($vyber11) {   
      $queryyy = mysql_query("SELECT funkcia, ID_funk FROM funkcie");
      while($row = mysql_fetch_array($queryyy)) {
         $v11=$row['ID_funk'];
         $k11=$row['funkcia'];
         $moznosti11 = '';
         if($vyber11==$v11){
            $moznosti11.='<option value="'.$v11.'" selected>'.$k11.'</option>';
         } else{
            $moznosti11.='<option value="'.$v11.'">'.$k11.'</option>';}
         }
        return $moznosti11;
     }
     if(isset($_POST['funkcia'])) {
        $selected11 = $_POST['funkcia'];
     }
   }
?> 
<select name="funkcia" class="ramceky" style="width: 150px; height: 40px" onchange="this.form.submit();">
   <option><?php echo get_options11($selected11); ?></option> 
</select>
  • 写回答

2条回答 默认 最新

  • doufa5001 2017-02-23 02:36
    关注

    You are assigning the variable $moznosti11; in every loop. Move it right before while loop and it should work like charm.

    See here:

    $moznosti11 = '';
    while($row = mysql_fetch_array($queryyy)) {
         $v11=$row['ID_funk'];
         $k11=$row['funkcia'];
    
         if($vyber11==$v11){
            $moznosti11.='<option value="'.$v11.'" selected>'.$k11.'</option>';
         } else{
            $moznosti11.='<option value="'.$v11.'">'.$k11.'</option>';}
         }
        return $moznosti11;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?