weixin_33739646 2015-08-19 08:01 采纳率: 0%
浏览 10

可靠的选择框

I have this code :

<form id="frmKat" role="form" action="" method="post">
    Country :
    <select name="country" id="country">
      <option>-select your country-</option>
    <?php 
    $result=mysqli_query($kon, "SELECT * from regios");
    while($country=mysqli_fetch_assoc($result)){         
    echo "<option value=$country[id]>$country[naam]</option>";

    } ?>
    </select>

    City :
    <select name="city" id="city">
        <option>-select your city-</option>
    </select>
    <div style="clear:both;"></div>
    <button type="submit" class="pull-right btn btn-success" name="btnConvert" id="btnConvert">Convert</button>
</form>

My ajax code is :

<script type="text/javascript">
       $(document).ready(function(){


           $("#country").change(function(){
                 var country=$("#country").val();
                 $.ajax({
                    type:"post",
                    url:"getcutoff.php",
                    data:"country="+country,
                    success:function(data){
                          $("#city").html(data);
                    }
                 });
           });
       });
  </script>

And my getcutoff.php file is next :

 <?php
session_start();
include("config.php");
global $kon;
ob_start();
if(isset($_SESSION["admin"])){ 

  $country=$_POST["country"];

  $result=mysqli_query($kon, "select * FROM cutoffs WHERE regio_id=". $country ." ");
  while($city=mysqli_fetch_array($result)){
    echo "<option value=$city[id]>$city[datetime]</option>";

  }
}else{
    ob_flush();
    die("Salut");   
}
?>

I'm trying to make dependable selectboxes. When user chooses something in the first selected box, then they will be taken to the second one where they can choose values which are connected to the first selected box. In first box I get values, but when I choose something in the second one, I get nothing. Where am I making a mistake?

  • 写回答

2条回答 默认 最新

  • weixin_33743248 2015-08-19 08:16
    关注

    Group your options in a php variable as string and use a single echo in getcutoff.php

    $res ='';
    while($city=mysqli_fetch_array($result)){ $res.="<option value=$city[id]>$city[datetime]</option>"; } 
    echo $res;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化