首页 > 解决方案 > 无法使用ajax从请求中获取数据

问题描述

尝试使用ajax从数据库请求中获取数据

正如你在下面看到的。代码是基本的ajax和sql请求代码,没有复杂的东西。但我的结果总是空的。

我需要帮助,谢谢

PS:对不起我的英语

阿贾克斯代码

$(document).ready(function(){
            $("#ligue").change(function(){
                var Ligue_Name=$(this).val();
                    $.ajax({
                        type: "POST",
                        url: "pages/test.php",
                        data: {Ligue_Name:Ligue_Name},
                        cache: false,
                        success: function(html){
                            $("#club").html(html);
                        } 
                    });
                });
        });

测试.php

<?php
    global $db;

    $output = '';

    $req = $db->query(
        "SELECT * FROM club WHERE Ligue_Name LIKE ".$_POST['Ligue_Name']."         
    ");

    $output .= '<option value="" disabled selected>Select Ligue</option>';
    if(mysqli_num_rows($req)>0){
        while ($row = mysqli_fetch_array($req)) {
            $output .= '<option value="'.$_POST["club"].'">'.$row["club"].'</option>';
        }
    }
    echo $output;
    
?>

标签: ajax

解决方案


Test.php

if($_POST["ligue"]){

        global $db;

        $output = '';

        $req = $db->query(
            "SELECT * FROM club WHERE Ligue_Name LIKE ".$_POST['Ligue_Name']."         
        ");

        $output .= '<option value="" disabled selected>Select Ligue</option>';
        if(mysqli_num_rows($req)>0){
            while ($row = mysqli_fetch_array($req)) {
                $output .= '<option value="'.$_POST["name"].'">'.$row["name"].'</option>';
            }
        }
        echo $output;
    }

my.html

<div class="input-group mb-3">
    <select class="custom-select" id="ligue" name="ligue">
        <option selected disabled>Selectionnez votre ligue</option>
        <?
        $clubs = getClub();
        foreach ($clubs as $club) {
        ?>
            <option value="<?php echo $club->Ligue_Name ?>"><?php echo $club->Ligue_Name ?></option>
        <?
            }

        ?>
    </select>
</div>

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <label class="input-group-text" for="club">Club</label>
  </div>
  <select class="custom-select" id="club" name="club" required>
  </select>
</div>
  <script>
    $(document).ready(function(){
            $("#ligue").change(function(){
                var Ligue_Name=$(this).val();
                    $.ajax({
                        type: "POST",
                        url: "pages/test.php",
                        data: {Ligue_Name:Ligue_Name},
                        cache: false,
                        success: function(html){
                            $("#club").html(html);
                        } 
                    });
                });
        });
  </script>

推荐阅读