我如何从php页面获取城市ID

时间:2012-05-14 13:14:34

标签: php

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

来自我的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>';

&GT;

这是我的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);
    }

}

1 个答案:

答案 0 :(得分:0)

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

或使用会话作为学习者的建议。

此外,如果您从HTML中提取JavaScript,代码将不那么难看......