第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

動(dòng)態(tài)下拉Ajax PHP 請(qǐng)求

動(dòng)態(tài)下拉Ajax PHP 請(qǐng)求

PHP
慕工程0101907 2023-08-19 16:29:13
我正在嘗試在表單中創(chuàng)建一個(gè)動(dòng)態(tài)填充的下拉列表以進(jìn)行位置選擇。我已經(jīng)在其他提出類似問(wèn)題的帖子和一些網(wǎng)站中搜索過(guò)堆棧,但我的第二個(gè)下拉列表始終為空。第一個(gè)下拉列表是通過(guò) 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>阿賈克斯請(qǐng)求<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貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

看來(lái)是數(shù)據(jù)庫(kù)的問(wèn)題。您可以使用mysqli_report(MYSQLI_REPORT_ALL);并查看發(fā)生了什么檢查ajax_county.php 最后的while,它可以創(chuàng)建無(wú)限循環(huán)。您正在使用 Database(),所以也許您可能會(huì)遇到問(wèn)題。


不管怎樣,我準(zhǔn)備了一個(gè)應(yīng)該適合你的代碼,


請(qǐng)注意,我再次修改了您的代碼,僅用于測(cè)試目的(僅使用 getCouncils() 函數(shù),并且數(shù)據(jù)庫(kù)查詢有點(diǎn)不同)


索引.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>

數(shù)據(jù)庫(kù).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>";

}


查看完整回答
反對(duì) 回復(fù) 2023-08-19
  • 1 回答
  • 0 關(guān)注
  • 114 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)