Canadian

  


 
     






Return to Previous Page












 

Retaining Selected Form Values

     Additions to Enable Retaining Value in Previous Drop Menu:
     You must first create a variable to capture the value passed to your second Query,
which is the item chosen in the first Drop Menu.


     <?php $cat = $colname_rspart ?> // $colname_rspart is the second query variable which was passed from the first form

     This is the first form on the page. The item chosen is passed to the second form on the page when the page is reloaded. $colname_rspart is the variable which contains this value. When the page is reloaded, we will check whether this vaiable has been set before, in which case, we need to assign that choice as selected in the first form. This has no effect on the outcome of using multiple forms, but it cosmetically makes your forms more professional.

     <?php
        do {
     ?>
     <option value="<?php echo $row_rsCategory['SubGroup']?>"
       <?php if ($row_rsCategory['SubGroup'] == $cat) {               // Check if this Item was previously Chosen
         echo 'selected="selected"';                                      // If it's a match, we designate this item as Selected
      } ?>><?php echo $row_rsCategory['SubGroup']?></option>
      <?php
         } while ($row_rsCategory = mysql_fetch_assoc($rsCategory));
         $rows = mysql_num_rows($rsCategory);
      if($rows > 0) {
         mysql_data_seek($rsCategory, 0);
           $row_rsCategory = mysql_fetch_assoc($rsCategory);
         }
     ?>

The added Code checks the items populating the menu against the chosen item passed to the second menu,
and if there is a match, selected is inserted into the option value for that item.