Hello! I really need help! I have these codes but I can't make them work.

In my first drop down list box, if I chose one option, I only wanted to see its appropriate value in the second drop down list box. For example, if I chose 'Quezon City' in the first drop down list box, I only wanted to see the areas in like, 'Ayala', 'Loyola', so on and so forth. But instead, my codes shows all the places even though I have already chosen one option! Please help! Thank you in advance!

Btw, the values in my drop down list boxes are in my database.

Select City:
    <form method="post" action="process.php">
    <label for="select"><select id="city" name="city" value="Select" size="1">

    <?php
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="gilmorewines"; // Database name 
        $tbl2="deldetails"; // Table name

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
        mysql_select_db("$db_name")or die("cannot select DB");

        $sql = "SELECT * FROM delcity"; 
        $result = mysql_query($sql) or die (mysql_error()); 

        while ($row = mysql_fetch_array($result))
        {
            $id=$row["city_id"];
            $hello = $_POST['city_id'];
            $cname=$row["cityname"];
            $options.="<OPTION VALUE=\"$id\">".$cname;
        }
    ?>

    <option>
        <? echo $options; ?>
    </option>
    </label></select><br>

Select Area:<br>
    <label for="select"><select name="area" value="Select" size="1"><br>

    <?php
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="gilmorewines"; // Database name 
        $tbl2="deldetails"; // Table name

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
        mysql_select_db("$db_name")or die("cannot select DB");

        if ($cname.value == "Quezon City"){
            $sql = "SELECT * FROM deldetails WHERE city LIKE 'q%'";
        }
        if ($cname.value == "San Juan City"){
            $sql = "SELECT * FROM deldetails WHERE city LIKE 's%'"; 
        }
        if ($cname.value == "Pasig City"){
            $sql = "SELECT * FROM deldetails WHERE city LIKE 'p%'"; 
        }
        if ($cname.value == "Taguig City"){
            $sql = "SELECT * FROM deldetails WHERE city LIKE 't%'"; 
        }
        else{
            $sql = "SELECT * FROM deldetails"; 
        }

        $result = mysql_query($sql) or die (mysql_error()); 
        while ($row = mysql_fetch_array($result))
        {
            $id2=$row["area_id"];
            $aname=$row["area"];
            $options2.="<OPTION VALUE=\"$id2\">".$aname;
        }
    ?>
    <option>
         <? echo $options2 ?>
    </option>
    </label> </select>

    <input type="submit" name="Submit" value="Submit">
    </form>
Member Avatar for diafol

You've mixed up html and php. Try to keep them separate. An ajax dropdown could help with this setup - that way you don't go through the whole rigamarol of page reload. If you want mysql instead of mysqli or PDO:

<?php
$link = mysql_connect('localhost','root','');
if (!$link) {
    die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('gilmorewines', $link);
if (!$db_selected) {
    die ('Can\'t use database : ' . mysql_error());
}

$op1 = '';
$op2 = '';
$first=0;   

$r=mysql_query("SELECT city_id, cityname FROM delcity ORDER BY cityname");
if(mysql_num_rows($r)){
    while($d = mysql_fetch_assoc($r)){
        if($first==0)$first=$d['city_id'];
        $op1 .= "\n\t<option value=\"{$d['city_id']}\">{$d['cityname']}</option>";    
    }
}

if(isset($_POST['city']))$first=intval($_POST['city']);

$r=mysql_query("SELECT area_id, area FROM deldetails WHERE city_id = $first ORDER BY area");
if(mysql_num_rows($r)){
    while($d = mysql_fetch_assoc($r)){
        $op2 .= "\n\t<option value=\"{$d['area_id']}\">{$d['area']}</option>";    
    }
}
?>

<form method="post" action="process.php">
<label for="select">City:</label>
<select id="city" name="city">
<?php echo $op1;?>
</select>
<label for="select">Area:</label>
<select id="area" name="area">
<?php echo $op2;?>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.