2012-04-15 9 views
0

私は2つのリストボックスを持っています国の最初の1つ、都市のもう1つ ユーザーは1つの国を選択すると国に基づいて都市を設定する必要があります最初のリストの選択された値に基づいてドロップダウンリストにデータを入力

郡のデータベースがある(CountryID(PK)、コード(PK)、名) 都市(国番号(PK)、名)

は、私が作成した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> 

答えて

0

ため このコードは、このような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> 

そして、それはspan要素の都市を出力getcities.php ....その国の名でgetcities.php呼び出し、フィールドの国を変更するにはここでは、この

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

ようなPHPファイルを書き込みます結果'。これはページをリロードする必要はありませんajax方法です。

関連する問題