Dependent dropdown list with mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doc1355
    New Member
    • Sep 2007
    • 16

    Dependent dropdown list with mysql

    Hi,
    I have two dynamic drop down lists: State and City.
    What I'm trying to do is first I need the city list to be empty on load. Then after a visitors selects the "state" from the first list, the page reloads and it shows all the available cities from my database where state is selected.
    Here is what I have sofar:

    [CODE=php]
    <?php require_once('C onnections/myConnection.ph p'); ?>
    <?php
    // Check for errors
    error_reporting (E_ALL);
    ini_set('displa y_errors', True);

    //

    if (!function_exis ts("GetSQLValue String")) {
    function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "", $theNotDefinedV alue = "")
    {
    $theValue = get_magic_quote s_gpc() ? stripslashes($t heValue) : $theValue;

    $theValue = function_exists ("mysql_real_es cape_string") ? mysql_real_esca pe_string($theV alue) : mysql_escape_st ring($theValue) ;

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValu e) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theV alue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValu e : $theNotDefinedV alue;
    break;
    }
    return $theValue;
    }
    }

    mysql_select_db ($database_myCo nnection, $myConnection);
    $query_state_re cordset = "SELECT DISTINCT `State` FROM programs ORDER BY `State` ASC";
    $state_recordse t = mysql_query($qu ery_state_recor dset, $myConnection) or die(mysql_error ());
    $row_state_reco rdset = mysql_fetch_ass oc($state_recor dset);
    $totalRows_stat e_recordset = mysql_num_rows( $state_recordse t);

    mysql_select_db ($database_myCo nnection, $myConnection);
    $query_city_rec ordset = "SELECT DISTINCT City FROM programs ORDER BY City ASC";
    $city_recordset = mysql_query($qu ery_city_record set, $myConnection) or die(mysql_error ());
    $row_city_recor dset = mysql_fetch_ass oc($city_record set);
    $totalRows_city _recordset = mysql_num_rows( $city_recordset );
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitl ed Document</title>
    </head>

    <body>
    <form id="form1" name="form1" method="post" action="">
    <label></label>
    <p>
    <label>State
    <select name="state_id" id="state_id">
    <option value=""></option>
    <?php
    do {
    ?>
    <option value="<?php echo $row_state_reco rdset['State']?>"><?php echo $row_state_reco rdset['State']?></option>
    <?php
    } while ($row_state_rec ordset = mysql_fetch_ass oc($state_recor dset));
    $rows = mysql_num_rows( $state_recordse t);
    if($rows > 0) {
    mysql_data_seek ($state_records et, 0);
    $row_state_reco rdset = mysql_fetch_ass oc($state_recor dset);
    }
    ?>
    </select>
    </label>
    </p>
    <p>
    <label>City
    <select name="city_ig" id="city_ig">
    <option value=""></option>
    <?php
    do {
    ?>
    <option value="<?php echo $row_city_recor dset['City']?>"><?php echo $row_city_recor dset['City']?></option>
    <?php
    } while ($row_city_reco rdset = mysql_fetch_ass oc($city_record set));
    $rows = mysql_num_rows( $city_recordset );
    if($rows > 0) {
    mysql_data_seek ($city_recordse t, 0);
    $row_city_recor dset = mysql_fetch_ass oc($city_record set);
    }
    ?>
    </select>
    </label>
    </p>
    </form>
    </body>
    </html>
    <?php
    mysql_free_resu lt($state_recor dset);

    mysql_free_resu lt($city_record set);
    ?>
    [/CODE]

    The page can be found HERE

    Thanks
    Last edited by ak1dnar; Sep 15 '07, 02:18 PM. Reason: Added CODE=php tags
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    There were some threads for this. Please use site search tool to find them. Search in "Php forum" with keywords "dependent drop down"
    Good Luck!
    -ajaxrand

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      [Refresh and filter record set]
      Thread Title changed to better describe the problem scenario.
      Please read here: Use a Good Thread Title

      Comment

      Working...