gpt4 book ai didi

php - 为什么我只能从MySQL中拉取 "id"属性而不能拉取 "name"

转载 作者:行者123 更新时间:2023-11-29 11:41:17 26 4
gpt4 key购买 nike

我有 4 个文件,如下所示:

PostVent.php:

<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8" />
<link rel="stylesheet" type="text/css" href="PostLinkStyle.css" />
<style type="text/css">
legend.standout{font-weight: bold; font-size: 24pt;}
</style>
<title>Post Vent</title>
</head>
<body>
<!-- start header div -->
<div id="header">
<h3>SomeVent</h3>
</div>
<div id="wrap">
<form action="" method="post">
<div class="row">
<div class="large-8 small-centered columns">
<fieldset>
<legend id="legend">Post Vent</legend>
<div class="row">
<div class="small-12 columns">
<label for="email">Email</label>
<input type="text" id="email" size="35"></input>
</div>
</div>
<div class="row">
<div class="small-8 columns">
<?php
//Include database configuration file
include('dbConfig.php');
include('index.php');
//Get all state data
$query = $db->query("SELECT **name** FROM states");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="state" id="state">
<option value="">Select State</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_array()){
echo '<option value="'.$row['name'].'">'.$row['id'].'</option>';
}
}else{
echo '<option value="">States not available</option>';
}
?>
</select>
<select name="county" id="county">
<option value="">Select County</option>
</select>
</div>
</div>

index.php:

<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'id='+stateID,
success:function(html){
$('#county').html(html);
}
});
}else{
$('#county').html('<option value="">Select state first</option>');
}
});
$('#county').on('change',function(){
var countyID = $(this).val();
if(countyID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'id='+stateID,
success:function(html){
$('#county').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>

ajax.php:

<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["id"]) && !empty($_POST["id"])){
//Get all county data
$query = $db->query("SELECT name FROM counties...");
//Count total number of rows
$rowCount = $query->num_rows;
//Display county list
if($rowCount > 0){
echo '<option value="">Select County</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
}else{
echo '<option value="">County not available</option>';
}
}
?>

加上我的数据库配置文件(我认为不需要显示它)。我正在尝试根据所选状态动态填充县下拉列表。

我对此很陌生,并将我从许多在线教程中获得的知识整合在一起。我的问题是这样的。为什么我的县菜单没有填充?我正在尝试将选定的州 id 属性发布到 ajax 以获取具有相同 id 的县。看不出有什么问题。

最佳答案

当然你不会得到你想要的,因为在你的 PostVent.php 中您显示的是 id隐藏值为 name 。应该是:

echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';

<option> 的语法是 <option value='[Specifies the value to be sent to a server]'>[Label or display value]</option>

您的查询也应该是:

SELECT id, name FROM states ORDER BY name

关于php - 为什么我只能从MySQL中拉取 "id"属性而不能拉取 "name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35739400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com