ajax - 无法使用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;
?>
解决方案
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>
推荐阅读
- r - 将命名数字向量拆分为 R 中的不同元素
- mysql - 如何计算周转日期时间?
- c# - c# Visual Studio mac 无法添加 sharepoint.client.runtime
- pandas - 根据另一列中的类别绘制一列中值的分布
- php - 通过php强制重新加载/刷新第二个网页
- javascript - Javascript 从本地 words.txt 2021 创建数组
- node.js - 如何从快速“路由器”中调用的另一个函数返回“res”?
- mongodb - MongoDb ObjectId 中的 4 字节时间戳值会溢出吗?
- java - 创建一个变量而不是多个 getter 使用——这对整体性能更好?
- php - Codeigniter 会话中如何、为什么以及在哪里使用表 ci_sessions?