在级联下拉列表中显示随时选择的数据

问题描述 投票:0回答:1

我已经制作了一个级联下拉列表,用于选择国家和城市(取决于所选国家/地区),以便用户更新其个人资料。

在早期版本中,用户选择国家和城市工作正常。但由于这将是一个更新配置文件页面,我想显示已选择的数据,如果数据不为空。

它适用于国家选择但无法弄清楚如何实现这个已经选择的数据来触发javascript,因为第二个dropbox仅在第一个下拉列表中选择时触发。

任何帮助表示赞赏!

这是javascript部分

<script language="javascript" type="text/javascript">
function getXMLHTTP() { //fuction 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(countryId) {       

    var strURL="findCountry.php?country="+countryId;
    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);
    }       
}
</script>

这是php部分:

    <?php
$username = $_SESSION[ 'username' ];
$sql = $conn->query( "SELECT * FROM users WHERE username='$username' " );

if ( $sql->num_rows > 0 ) {
    // output data of each row
    while ( $info = $sql->fetch_assoc() ) {
        echo '

                        <select id="country" name="country" onchange="getCity(this.value)"  required>
                <option value="" selected disabled hidden>Country</option>';
        $sql2 = $conn->query( "SELECT * FROM world_countries ORDER BY country_name ASC" );
        // output data of each row
        if ( $info[ 'country' ] != null ) {
            echo '<option value="' . $info[ "country" ] . '" selected>' . $info[ "country" ] . '</option>';
        }
        while ( $row = $sql2->fetch_assoc() ) {
            echo '<option value="' . $row[ "cc_iso" ] . '">' . $row[ "country_name" ] . '</option>';
        }

        echo '<div id="citydiv"><select name="city"><option>Select Country First</option></select></div>';
    }}
        ?>

这是findCountry.php:

<? 
require_once("config.php");
$country=$_GET['country'];
$sql=$conn->query("SELECT * FROM world_cities WHERE id='$country' ORDER BY full_name_nd ASC");
if ( $sql->num_rows > 0 ) {
echo '
<select name="cities">
<option>Select City</option>';
while($row=$sql->fetch_assoc()) { 
echo '<option value="'.$row['full_name_nd'].'">'.$row['full_name_nd'].'</option>';
}
echo '</select>';
} else {
echo 'error';
}
$conn->close();
?>
php cascade cascadingdropdown
1个回答
0
投票

您应该向ajax请求添加第二个GET参数,以获取已选择的选项,例如:

function getCity(countryId, selected = "none") {
    var strURL="findCountry.php?country="+countryId+"&selected="+selected;
    var req = getXMLHTTP();
    //...

在您的PHP添加选择正确的选项:

<? 
require_once("config.php");
$country=$_GET['country'];
$selected = $_GET['selected'];
$sql=$conn->query("SELECT * FROM world_cities WHERE id='$country' ORDER BY full_name_nd ASC");
if ( $sql->num_rows > 0 ) {
echo '
<select name="cities">
<option>Select City</option>';
while($row=$sql->fetch_assoc()) { 
$status = "";
if($selected!="none" && $row['full_name_nd'] == $selected) //apply selection
    $status = "selected";
echo '<option value="'.$row['full_name_nd'].'" '.$status.'>'.$row['full_name_nd'].'</option>';
}
echo '</select>';
} else {
echo 'error';
}
$conn->close();

现在,您应该能够在页面加载时调用您的函数来传递选定的城市

© www.soinside.com 2019 - 2024. All rights reserved.