[英]Can't get data from request with ajax
嘗試使用 ajax 從數據庫請求中獲取數據
正如您在下面看到的那樣。 代碼是基本的ajax和sql請求代碼,沒有什么復雜的東西。 但我的結果總是 null。
我需要幫助,謝謝
PS:對不起我的英語
Ajax代碼
$(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;
?>
您的 HTML 代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div id="club"></div><br />
<input type="text" id="ligue" >
<button type="button" id="addline" onclick="league();">+</button>
<script>
function league(){
var Ligue_Name=$("#ligue").val();
$.ajax({
type: "POST",
url: "pages/test.php",
data: {Ligue_Name:Ligue_Name},
cache: false,
success: function(html){
$("#club").html(html);
}
});
};
</script>
</body>
</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="Testing">'.$row["club"].'</option>';
}
}
echo $output;
?>
測試.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;
}
我的.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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.