Jump to content


This topic is now archived and is closed to further replies.


PHP & JavaScript & MySQL Help

Recommended Posts


I have 2 MySQL tables, 'Main Category', and 'Sub-Category.'

Basically, the Main Category is the main/parent category, and the Sub-Category is a child of the main. In Sub-Category, there's 3 fields, subid, maincatid, and name. What I want is 2 dynamic dropdown menus. The first one, makes a query and gets all the Main Categories AND all the rows. Then, another query is made. The new query gets all the Sub-Categories thats field, 'maincatid' is the chosen one from the first drop-down menu. The second drop-down menu is updated, grabbing all of the correct Sub-Categories. Sorry, I am a bad explainer. If you are confused on what I am saying, please contact me through either this thread, my AIM, or my MSN. (All in my profile)


Help would be GREATLY appreciated.

P.S. This is what I have so far:

[code]<select name="category">
        <option value="Default">Choose A Category:</option>
        <?php $query = "SELECT * FROM category";
    $result = mysql_query ($query);
    while ($row = mysql_fetch_array
        ($result, MYSQL_NUM)) {
echo "<option value='{$row[0]}'>{$row[0]}</option>";} ?>
    <td align="right" valign="bottom">&nbsp;</td>
    <td><select name="subcategory">
        <option value="Default">Choose A Sub-Category:</option>
        <?php $query = "SELECT * FROM subcategory WHERE maincatid = 'AHHHH! Help!'";
    $result = mysql_query ($query);
    while ($row = mysql_fetch_array
        ($result, MYSQL_NUM)) {
echo "<option value='{$row[0]}'>{$row[0]}</option>";} ?>

Share this post

Link to post
Share on other sites
I assume the $row[0] will return the ID, or you can use $row['You ID field here'] instead

Try the code below, hope it helps

$query = "SELECT * FROM category";
$result = mysql_query ($query);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    $main_cats .= "<option value='".$row[0]."'>".$row[0]."</option>";
    $query1 = "SELECT * FROM subcategory WHERE maincatid = '".$row[0]."'";
    $result1 = mysql_query ($query1);
    $sub_arrays .= "subs[".$row[0]."] = new Array(";
    while ($row1 = mysql_fetch_array($result1, MYSQL_NUM)) {
        $sub_arrays .= "\"".$row1[0]."\",";
    $sub_arrays = trim($sub_arrays, ",") .");\n";



<script language="javascript">
var subs = new Array();

<?= $sub_arrays ?>

function fill_subs(val){
    if (val != ""){
        var sub_sel = document.getElementById('sub_select');

        opt = document.createElement("OPTION");
        opt.value = "";
        opt.innerHTML = "Select";
        for(i=0; i<subs[val].length; i++){
            opt = document.createElement("OPTION");
            opt.value = subs[val][i];
            opt.innerHTML = subs[val][i];

<select onChange="fill_subs(this.value);">
    <?= $main_cats ?>

<select id="sub_select" name="sub_select">
    <option value="">Select</option>

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.