动态下拉Ajax PHP 请求

我正在尝试在表单中创建一个动态填充的下拉列表以进行位置选择。我已经在其他提出类似问题的帖子和一些网站中搜索过堆栈,但我的第二个下拉列表始终为空。


第一个下拉列表是通过 MySQL 查询填充的。


表格部分


<label for="" class="block">District

    <select id="dists" name="prop_district" class="full block" required>

        <option selected disabled>District...</option>

        <?php

            $dist = new Database();

            $dist->getDistricts();

        ?>

    </select>

</label>


<label for="" class="block">Council

    <select id="p_councils" name="prop_council" class="full block" required>

        <option selected disabled>Council...</option>


    </select>

</label>


阿贾克斯请求


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>

    $(document).ready(function(){

        $("#dists").change(function(){

        var id=$(this).val(); 

        

        $.ajax({

                    type: "GET",

                    url: "includes/scripts/ajax/ajax_county.php",

                    data: { district : $("#dists").val() },

                    success: function(reply){

                        $("#councils").html(reply);

                        console.log(reply);

                    },

                    error: function() {

                        alert('Error occured');

                    }

            });

            

        });

    });

</script>


ajax_county.php


<?php

if(isset($_POST['district'])){

    $district = $_POST['district'];


    $dist = new Database();

    $dist->getCouncils($district);


}else{

    echo"fail";

}


?>


慕工程0101907
浏览 85回答 1
1回答

小唯快跑啊

看来是数据库的问题。您可以使用mysqli_report(MYSQLI_REPORT_ALL);并查看发生了什么检查ajax_county.php 最后的while,它可以创建无限循环。您正在使用 Database(),所以也许您可能会遇到问题。不管怎样,我准备了一个应该适合你的代码,请注意,我再次修改了您的代码,仅用于测试目的(仅使用 getCouncils() 函数,并且数据库查询有点不同)索引.php<?phpinclude('Database.php');?><label for="" class="block">District&nbsp; &nbsp; <select id="dists" name="prop_district" class="full block" required>&nbsp; &nbsp; &nbsp; &nbsp; <option selected disabled>District...</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dist = new Database();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dist->getCouncils(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select></label><label for="" class="block">Council&nbsp; &nbsp; <select id="p_councils" name="prop_council" class="full block" required>&nbsp; &nbsp; &nbsp; &nbsp; <option selected disabled>Council...</option>&nbsp; &nbsp; </select></label><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script>&nbsp; &nbsp; $(document).ready(function(){&nbsp; &nbsp; &nbsp; &nbsp; $("#dists").change(function(){&nbsp; &nbsp; &nbsp; &nbsp; var id=$(this).val();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "ajax_county.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: { district: id },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(html){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(html);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#p_councils").html(html);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>数据库.php<?phpclass Database{public static $host = "localhost";public static $dbName = "yourdatabase";public static $username = "root";public static $password = "";private static function connect() {&nbsp; &nbsp; $pdo = new PDO("mysql:host=".self::$host.";dbname=".self::$dbName.";charset=utf8", self::$username, self::$password);&nbsp; &nbsp; $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);&nbsp; &nbsp; return $pdo;}//GET COUNCILS&nbsp;&nbsp;&nbsp; &nbsp; public static function getCouncils($id) {&nbsp; &nbsp; &nbsp; &nbsp; $con = new Database();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $con->connect();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt = self::connect()->prepare("SELECT * FROM councils where council_id = $id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $councils = $stmt->fetchAll();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($councils as $row):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<option value=".$row['council_id'].">".$row['council_name']."</option>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endforeach;&nbsp;&nbsp; &nbsp; }}ajax_county.php<?php$dist = $_POST['district'];$servername = "localhost";$username = "root";$password = "";//mysqli_report();// Create connection$con = new mysqli($servername, $username, $password);$con->select_db("teststack");$stmt = $con->prepare("SELECT * FROM councils WHERE dist_parent_id = ?");$stmt->bind_param( 'i' , $dist);$stmt->execute();$dists = $stmt->get_result();$dists = $dists->fetch_all(MYSQLI_ASSOC);echo '<option selected disabled>Councils...</option>';foreach($dists as $r){&nbsp; &nbsp; echo "<option value=".$r['council_id'].">".$r['council_name']."</option>";}
打开App,查看更多内容
随时随地看视频慕课网APP