Skip to main content
July 30, 2009
Answered

Dependent dropdown menu problem

  • July 30, 2009
  • 1 reply
  • 1230 views

I have 3 dependent dropdown menues setup in dreamweaver that gets their data from a MySQL Table.

When I use dreamweaver ot control the dropdown they work fine, but the data being fed into my db is the numeric value used to control the dropdown not the label. If I change the value of the dropdown in dreamweaver to the lable the dropdown arent dependent anymore.

I heard that I have to set Foreign Keys to fix this.

I would apreciate any help I can get.

My first table is province:

CREATE TABLE `db`.`province` (
  `id_prov` INTEGER UNSIGNED NOT NULL DEFAULT NULL AUTO_INCREMENT,
  `name_prov` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id_prov`)
)
ENGINE = InnoDB;

Second is City:

CREATE TABLE `db`.`city` (
  `id_city` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `idprov_city` INTEGER UNSIGNED NOT NULL,
  `name_city` VARCHAR(65) NOT NULL,
  PRIMARY KEY (`id_city`)
)
ENGINE = InnoDB;

Third is Suburb:

CREATE TABLE `db`.`suburb` (
  `id_suburb` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `idprov_suburb` INTEGER UNSIGNED NOT NULL,
  `idcity_suburb` INTEGER UNSIGNED NOT NULL,
  `name_suburb` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id_suburb`)
)
ENGINE = InnoDB;

This topic has been closed for replies.
Correct answer

Province:

<option value="<?php echo $row_province['id_prov']?>"><?php echo $row_province['name_prov']?></option>

From the code above, you are storing the id_prov which i believe it was an integer type (value="<?php echo $row_province['id_prov']?>"). If you want to store the name_prov attribute, u should replace id_prov to name_prov.

But as u said, if u store the name_prov, the other drop down menu is not dependent anymore. It's true because from the city table, there is one attribute i guess was a foreign key (idprov_city) and u set it as int. That's why if u use id_prov(int), the dependent works fine.

1 reply

July 31, 2009

When u choose drop down menu, u may see that there are 2 important fields, values and label. The values field will store the value in database while the label field will only show the list to be chosen by users. What value that u want to store in databse?

July 31, 2009

Here is the code.

I want to insert the lable into the database.

I am using the value to control the dropdown.

Province:

<tr>
      <td class="KT_th"><label for="province">Province:</label></td>
      <td><select name="province" id="province">
        <?php
do { 
?>
        <option value="<?php echo $row_province['id_prov']?>"><?php echo $row_province['name_prov']?></option>
        <?php
} while ($row_province = mysql_fetch_assoc($province));
  $rows = mysql_num_rows($province);
  if($rows > 0) {
      mysql_data_seek($province, 0);
      $row_province = mysql_fetch_assoc($province);
  }
?>
      </select>
        <?php echo $tNGs->displayFieldError("private_buyer", "province"); ?> </td>
    </tr>

City:

<tr>
      <td class="KT_th"><label for="city">City:</label></td>
      <td><select name="city" id="city" wdg:subtype="DependentDropdown" wdg:type="widget" wdg:recordset="city" wdg:displayfield="name_city" wdg:valuefield="id_city"  wdg:fkey="idprov_city" wdg:triggerobject="province" >
    </select>
          <?php echo $tNGs->displayFieldError("private_buyer", "city"); ?> </td>
    </tr>

Suburb:

<tr>
      <td class="KT_th"><label for="suburb">Suburb:</label></td>
      <td><select name="suburb" id="suburb" wdg:subtype="DependentDropdown" wdg:type="widget" wdg:recordset="suburb" wdg:displayfield="name_suburb" wdg:valuefield="name_suburb" wdg:fkey="idcity_suburb" wdg:triggerobject="city">
    </select>
          <?php echo $tNGs->displayFieldError("private_buyer", "suburb"); ?> </td>
    </tr>

Correct answer
August 3, 2009

Province:

<option value="<?php echo $row_province['id_prov']?>"><?php echo $row_province['name_prov']?></option>

From the code above, you are storing the id_prov which i believe it was an integer type (value="<?php echo $row_province['id_prov']?>"). If you want to store the name_prov attribute, u should replace id_prov to name_prov.

But as u said, if u store the name_prov, the other drop down menu is not dependent anymore. It's true because from the city table, there is one attribute i guess was a foreign key (idprov_city) and u set it as int. That's why if u use id_prov(int), the dependent works fine.