2012-05-14 53 views
0

我正在为我的网站创建注册表单。在我的注册表格中,有两个选择框可以选择用户所在的地区和城市。所以我需要这样做,当用户选择他们的地区,然后自动显示与上述选定地区相关的城市的城市选择框。为此,我使用AJAX和PHP。我用findcity.php页面在我的register.php页面显示城市。我的问题是,当我尝试从register.php页面获得城市id我无法得到它。它需要使用其他数据从register.php页面发送到数据库。如何从php页面获取城市ID

从我register.php页面

<div> 
<label for="district">District <img src="../images/required_star.png" alt="required" /> : </label> 
<?php 

    require_once ('../includes/config.inc.php');  
    require_once(MYSQL2); 

    $query="select * from district order by district_id"; 
    $result = mysqli_query($dbc, $query); 

     echo '<select name="district" class="text" onChange="getCity(' . "'" . 'findcity.php?district=' . "'" . '+this.value)">'; 
     echo '<option value="">-- Select District --</option>'; 

     while($row = mysqli_fetch_array($result, MYSQLI_NUM)) { 
      echo '<option value="' . $row[0] . '"'; 

      // Check for stickyness: 
      if (isset($_POST['district']) && ($_POST['district'] == $row[0]))  
       echo ' selected="selected"'; 

       echo " >$row[1]</option>";  
     } 
     echo '</select>'; 
?> 

</div>  
<div> 
    <label for="city">City <img src="../images/required_star.png" alt="required" /> : </label> 
    <input type="hidden" name="reg_locationid" id="reg_locationid" value="56" /> 
    <div id="citydiv" style="position: relative; top: -14px; left: 130px; margin-bottom: -26px;"> 
     <select name="city" class="text"> 
      <option>-- Select City --</option> 
     </select> 
    </div> 
</div> 

这是findcity.php页

<?php 

$districtId=$_GET['district']; 

require_once ('../includes/configaration.inc.php'); 
require_once(MYSQLCONNECTION); 

$query="select city_id, city_name from city2 where district_id=$districtId"; 
$result=mysqli_query($dbc, $query); 



echo '<select name="city" class="text"> 
     <option>-- Select City --</option>'; 

while($row=mysqli_fetch_array($result, MYSQLI_NUM)) { 

    echo '<option value="' . $row[0] . '"'; 

    // Check for stickyness: 
    if (isset($_POST['city']) && ($_POST['city'] == $row[0])) { 
     echo ' selected="selected"'; 

     //echo '<input type="hidden" name="city" value="' . $row[0] . '"'; 

    } 
     echo " >$row[1]</option>"; 

} 

echo '</select>'; 

?>

这是我的AJAX功能

function getXMLHTTP() { //function to return the xml http object 
    var xmlhttp=false; 
    try{ 
     xmlhttp=new XMLHttpRequest(); 
    } 
    catch(e) {  
     try{    
      xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     catch(e){ 
      try{ 
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
      catch(e1){ 
       xmlhttp=false; 
      } 
     } 
    } 

    return xmlhttp; 
} 



function getCity(strURL) {  

    var req = getXMLHTTP(); 

    if (req) { 

     req.onreadystatechange = function() { 
      if (req.readyState == 4) { 
       // only if "OK" 
       if (req.status == 200) {       
        document.getElementById('citydiv').innerHTML=req.responseText;      
       } else { 
        alert("There was a problem while using XMLHTTP:\n" + req.statusText); 
       } 
      }    
     }   
     req.open("GET", strURL, true); 
     req.send(null); 
    } 

} 
+0

您可以使用会话... –

回答

0
echo '<select name="district" class="text" onChange="getCity(' . 
     '\'findcity.php?district=\'+this.value+\'&city=\'' . 
     '+document.getElementsByName(\'city\')[0].value)">'; 

或按照学习者的建议使用会话。

此外,如果您从HTML中提取JavaScript,代码将会更加难看...