我正在尝试创建一个程序,当您从下拉菜单中选择一个州时,它将在另一个下拉菜单中显示该州的城市列表。选择城市和州后,您输入一个地址,然后单击“提交”,它将在新的php文件中显示完整地址。

例如,如果从下拉菜单中选择“新泽西州”,则其左侧的下拉菜单应显示三个城市:纽瓦克,布卢姆菲尔德和爱迪生。

目前,我的问题是可以显示各州,但是选择州后,它没有在第二个下拉菜单中提供该城市的选项列表。任何帮助表示赞赏,谢谢!

您可以在此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: 'get',
 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 value='".$row['state']."'>".$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 = $_GET['get_option'];
 $find=mysql_query("select city from zipcodes where state='$state'");
 while($row=mysql_fetch_array($find))
 {
  echo "<option>".$row['city']."</option>";
 }
 exit;
}
?>

最佳答案

当您检查get_option是否发布时,您的ajax是“ get”。

更改为

if(isset($_GET['get_option']))

关于javascript - Ajax,多个下拉菜单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43711502/

10-14 14:30