基本上我是一个刚开始学习PHP和MySQL的新资源管理器,我要做的是使依赖项下降。 好的第一个下拉列表不错,但是第二个下拉列表与第一个下拉列表没有关联,有人可以帮忙吗?
谢谢。
index.php
<?php
$connect = new mysqli ("localhost", "root", "root", "carsdb");
?>
<!doctype html>
<html lang="en">
<head>
<body>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>Car Selection</title>
<div class = "container" style = "width:700px;">
<form action = "" method = "POST">
<div class = "jumbotron">
<h1>Car Selection</h1><br>
<label>Brand:</label> <select id = "Brand">
<option value = "">Car Brand Selection</option>
<?php
$query = "SELECT Brand FROM Brand";
$result = $connect->query($query);
while ($row = $result->fetch_assoc())
{
echo "<option value = '{$row["Brand"]}'>{$row["Brand"]}</option>";
}
?>
</select><br>
<label>Model:</label> <select id = "Model">
<option value = "">Car Model Selection</option>
</select><br><br>
<input type = "submit" name="insert" class = "btn btn-success btn-sm" value="Next">
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#Brand").change(function(){
bid=$(this).val();
$.post("fetch.php",{id:bid}, function(data){
$("#Model").html(data);
});
});
</script>
</body>
</html>
fetch.php
<?php
$connect = new mysqli ("localhost", "root", "root", "carsdb");
$query = "SELECT Model FROM Model where CODE = {$_POST["id"]}";
$result = $connect->query($query);
echo "<option value = ''>Select</option>";
while ($row = $result->fetch_assoc())
{
echo "<option value = '{$row["Code"]}'>{$row["Model"]}</option>";
}
?>
答案 0 :(得分:0)
而不是-
echo "<option value = '{$row["Code"]}'>{$row["Model"]}</option>";
尝试----
echo '<option value="'.$row['Code'].'">'.$row['Model'].'</option>';