为了账号安全,请及时绑定邮箱和手机立即绑定

动态下拉Ajax PHP 请求

动态下拉Ajax PHP 请求

PHP
慕工程0101907 2023-08-19 16:29:13
我正在尝试在表单中创建一个动态填充的下拉列表以进行位置选择。我已经在其他提出类似问题的帖子和一些网站中搜索过堆栈,但我的第二个下拉列表始终为空。第一个下拉列表是通过 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<?phpif(isset($_POST['district'])){    $district = $_POST['district'];    $dist = new Database();    $dist->getCouncils($district);}else{    echo"fail";}?>
查看完整描述

1 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

看来是数据库的问题。您可以使用mysqli_report(MYSQLI_REPORT_ALL);并查看发生了什么检查ajax_county.php 最后的while,它可以创建无限循环。您正在使用 Database(),所以也许您可能会遇到问题。


不管怎样,我准备了一个应该适合你的代码,


请注意,我再次修改了您的代码,仅用于测试目的(仅使用 getCouncils() 函数,并且数据库查询有点不同)


索引.php


<?php

include('Database.php');

?>


<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->getCouncils(1);

            

        ?>

    </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: "POST",

            url: "ajax_county.php",

            data: { district: id },

            success: function(html){

                console.log(html);

                $("#p_councils").html(html);

            } 

            });

            

        });

    });

</script>

数据库.php


<?php

class Database{

public static $host = "localhost";

public static $dbName = "yourdatabase";

public static $username = "root";

public static $password = "";


private static function connect() {

    $pdo = new PDO("mysql:host=".self::$host.";dbname=".self::$dbName.";charset=utf8", self::$username, self::$password);

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    return $pdo;

}


//GET COUNCILS  

    public static function getCouncils($id) {

        $con = new Database();

            $con->connect();

            $stmt = self::connect()->prepare("SELECT * FROM councils where council_id = $id");

            $stmt->execute();

            

            $councils = $stmt->fetchAll();

            foreach ($councils as $row):

            echo "<option value=".$row['council_id'].">".$row['council_name']."</option>";

            endforeach; 

    }

}

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){

    echo "<option value=".$r['council_id'].">".$r['council_name']."</option>";

}


查看完整回答
反对 回复 2023-08-19
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信