【发布时间】:2017-04-28 17:34:59
【问题描述】:
我正在尝试创建一个程序,当您从下拉菜单中选择一个州时,它会在另一个下拉菜单中显示该州的城市列表,您可以从中进行选择。选择城市和州后,输入地址,点击提交,它将在新的 php 文件中显示完整地址。
My issue at the moment is I can get the states displayed, but when the state is selected, it is not giving me the list of options for that city in the second drop down menu.任何帮助表示赞赏,谢谢!
你可以在link查看行为
select.php
<head>
<link rel="stylesheet" type="text/css" href="select_style.css">
<script type="text/javascript" src="js/jquery.js"></script>
<!DOCTYPE html>
<form action = "display.php">
<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'fetch.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("new_select").innerHTML=response;
}
});
}
</script>
</head>
<body>
<p id="heading">Address Generator</p>
<center>
<div id="select_box">
<select onchange="fetch_select(this.value);">
<option>Select state</option>
<?php
include ( "accounts.php" ) ;
( $dbh = mysql_connect ( $hostname, $username, $password ) )
or die ( "Unable to connect to MySQL database" );
print "Connected to MySQL<br>";
mysql_select_db( $project );
$select=mysql_query("select state from zipcodes group by state");
while($row=mysql_fetch_array($select))
{
echo "<option>".$row['state']."</option>";
}
?>
</select>
<select id="new_select">
</select>
<div id='2'> </div>
<br><br>
<input type = text name="address">Address
<br><br>
<input type = submit>
</form>
fetch.php
<?php
include(accounts.php);
if(isset($_POST['get_option']))
{
( $dbh = mysql_connect ( $hostname, $username, $password ) )
or die ( "Unable to connect to MySQL database" );
print "Connected to MySQL<br>";
mysql_select_db( $project );
$state = $_POST['get_option'];
$find=mysql_query("select city from zipcodes where state='$state'");
while($row=mysql_fetch_array($find))
{
echo "<option>".$row['city']."</option>";
}
exit;
}
?>
【问题讨论】:
-
mysql_* 已弃用尝试使用 mysqli_*
-
ajax 响应返回什么可以用控制台查看
-
首先你的 state 选项的 value 属性缺少 echo "
-
并包含(accounts.php); account.php 应该用双引号括起来
-
@JYoThI 我应该在哪里插入那行代码?如果这是一个愚蠢的问题,我深表歉意,但我在使用 AJAX 方面还很陌生