簡體   English   中英

根據第一個列表的選定值填充下拉列表

[英]populating the drop down list based on the selected value of first list

我有2個列表框,第一個列表用於國家/地區,另一個列表用於城市,當用戶選擇一個國家/地區時,它應基於該國家/地區填充城市

縣的數據庫是(CountryID(pk),Code(pk),Name)city(CountryCode(pk),Name)

我創建了一個名為reload的Java腳本函數,該函數可以加載頁面onChange,使其工作正常,但城市列表中的問題未包含國家(地區)項目。它仍然為空。這是我的代碼。 我只是張貼與問題有關的代碼,它的頁面太長。 此代碼頁

dd-check.php

<?php
$cat = $_GET['Country'];
$subcat = $_POST['City'];
?>

CreateAccount.php

<script language=JavaScript>
function reload(form)
{
  var val = form.Country.options[form.Country.options.selectedIndex].value; 
  self.location = 'Create_Account.php?country=' + val ;
}
</script>

<form id="form2" method="post"  enctype="multipart/form-data" action="dd-check.php">
<?php
$Con= mysql_connect("localhost","root",""); 

if(!$Con) 
{ 
  die('Could not connect'.mysql_error());
}

if(!mysql_selectdb("rlounge",$Con))
{
  die(mysql_error());
}

@$cat = $_GET['Country'];

if(strlen($cat) > 0 and !is_numeric($cat))
{  
  echo "Data Error";
  exit;
}

$quer2 = "SELECT *  FROM country";
$result = mysql_query($quer2);
if(isset($cat) and strlen($cat) > 0)
{
  $quer = mysql_query(" SELECT city.`Name` ,  `CountryCode` 
                        FROM  `city` ,  `country` 
                        WHERE  `CountryCode` =  $cat
                        AND  `Code` =  `CountryCode` "); 
}
else
{
  $quer = mysql_query(" SELECT City.name
                      FROM  `city` ,  `country` 
                      WHERE  `Code` =  `CountryCode`"); 
} 
//$cat=$_GET['Country'];
//$subcat=$_POST['City'];
echo "<select name='Country' onchange=\"reload(this.form)\"><option value=''>Select     one</option>";
while($noticia2 = mysql_fetch_array($result)) 
{ 
  if($noticia2['Code'] == $cat)
  {
    echo "<option selected value='$noticia2[Code]'>$noticia2[Name]</option>"."<BR>";
  }
  else
  {
    echo "<option value='$noticia2[Code]'>$noticia2[Name]</option>";
  }
}
echo "</select>";
echo "<select name='City'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) 
{ 
  echo  "<option value='$noticia[CountryCode]'>$noticia[Name]</option>";
}
echo "</select>"; 
?>
</form>

創建一個這樣的html文件。

<html>
<head>
<script type="text/javascript">
function getcities(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("result").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getcities.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<select name='Countries' onchange='getcities(this)'>
</select>
<p> <span id="result"></span></p>

</body>
</html>

然后像這樣寫php文件

<?php
$country=$_GET['q'];
get cities from database
while(cities)
{
    echo "<option>".$city."</option>";
}
?>

在更改字段國家/地區時,它將使用該國家/地區名稱調用getcities.php。...getcities.php輸出span元素“結果”中的城市。 這是無需重新加載頁面的ajax方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM