2012-04-15 6 views
0

Ich habe 2 Listenfeld das erste für das Land, das andere für die Stadt , wenn der Benutzer ein Land wählen sollte es die Stadt basierend auf dem LandAuffüllen der Dropdown-Liste basierend auf dem ausgewählten Wert der ersten Liste

bevölkern

die Datenbank für die Grafschaft ist (CountryID (pk)-Code (pk), Name) Stadt (Countrycode (pk), Name)

i erstellen Java-Script-Funktion namens reload, der die Seite onChange seine Arbeits laden gut, aber das Problem in der Stadt Liste ist nicht mit dem Land Artikel bevölkert .. es ist immer noch leer .. hier ist mein Code. Ich poste gerade den Code, der sich auf das Problem bezieht, seine zu lange Seite. dieser Code für Seite

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> 

Antwort

0

eine HTML-Datei wie folgt erstellen.

<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> 

Dann PHP-Datei wie diese

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

hier zum Ändern des Feldes Land ruft getcities.php mit diesem Land Name .... getcities.php gibt die Städte in der Spanne Element schreiben ' Ergebnis'. Dies ist eine Ajax-Methode, bei der Sie die Seite nicht neu laden müssen.

Verwandte Themen