亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動態下拉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 關注
  • 107 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號