如何使用ajax创建另一个下拉列表?

时间:2015-08-11 13:10:39

标签: php html ajax

你好我想当用户选择伊朗旁边的那个加载城市而不加载!

<select id="select2" name="street" class="cty street">
    <option>Iran</option>
    <option>Japan</option>
    <option>UnitedState</option>
</select>

<script type="text/javascript">
    $(document).ready(function(){
        $("#select2").click(function(){
            var key= $(".street").val();
            var shahrha=$("#select2 option:selected").text();
            var active =true;
            $.get("ajax.php", {key:key,shahrha:shahrha,active:active},function(data){
            $(".street").html(data); 
        });
    });
</script>

// ------ ajaxpage ----- //

  if(isset($_GET["active"]) && isset($_GET["key"]) && isset($_GET["shahrha"])){
    $keyword=$_GET["key"];
    $shahr=$_GET["shahrha"];
    $sql="SELECT * FROM `tbl_detail` WHERE `street` like :name AND `shahr`=:sh";
    $result=$connect->prepare($sql);
    $k="%".$keyword."%";
    $result->bindParam(":sh",$shahr);
    $result->bindParam(":name",$k);
    $run=$result->execute();
    if($run){
    while($rows=$result->fetch(PDO::FETCH_ASSOC)){
    echo "<select class='cty'><option>".$rows["street"]."</option></select>";
            }
        }
        else{
        echo "dosnt exist";

        }


    }

我想用ajax做到这一点!我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

将代码的最后一部分设为如下

echo "<select class='cty'>";
while($rows=$result->fetch(PDO::FETCH_ASSOC)){

        echo "<option>".$rows["street"]."</option>";
}
echo "</select>";