Skip to content Skip to sidebar Skip to footer

In Php And Jquery, Make One Drop-down Depend On Another Drop-down List?

I have two drop down lists one is depended on other if one value selected the other one load same values from database. for example if i select one country other load same cities t

Solution 1:

You're looking for "ajax" functionality. Try looking into $.get(url,data,success); using JQuery

First, remove dropbox B and replace it with a div with id="B"

$("#A").change(loadCities);
functionloadCities(e){
    // prepare get statementvar url = "http://www.yoursite.com/ajax/getCities";
    var data = {
        country : $("#A").val()
    };
    $.get(url, data, loadCitiesComplete);
}
functionloadCitiesComplete(data){
    $("#B").html(data);
}

That url : "http://www.yoursite.com/ajax/getCities" php should look something like

<?if(isset($_GET['country'])){
    $html = '<select name="B" class="input_text" id="B">';
    include'config/config.php'; 
    $dpttitle = mysql_real_escape_string($_GET['country']);
    $sql="SELECT * FROM department WHERE DeptCode=$dpttitle";
    $result=mysql_query($sql);$options="";
    while ($row=mysql_fetch_array($result)){    
    $did=$row["DeptCode"];
    $depts=$row["Dept"];
    $html .="<OPTION value='$did'>".$depts;}?>$html .= '<option value="0">Select...</option>';
    $html .= '</option>';
    $html .= '</select>';
    echo$html;
}
?>

Obviously the php should use the country $_GET var properly, use PDO or MySQLi with prepared statements for safety, etc. But hopefully this'll get you moving in the right direction.

Post a Comment for "In Php And Jquery, Make One Drop-down Depend On Another Drop-down List?"