Jump to content

Archived

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

drranch

How would one script for a dynamic list to keep selected item in list

Recommended Posts

I have a dynamic list that once an item is selected the form retrieves the data from the database, but when the form is refreshed the list defaults back to the defaulted item specified in the script.

Is there a way to have the list stay on the item selected rather than defaulting to the specified default in the script?

Here is my dynamic list script:
<SELECT name="cars" size="1" onchange=cars.submit()>
    <option >Select a car</option>
    <?php
do { 
?>
    <option value="<?php echo $row_rcars['cars']?>"><?php echo $row_cars['car']?></option>
    <?php
} while ($row_cars= mysql_fetch_assoc($rscars));
  $rows = mysql_num_rows($rscars);
  if($rows > 0) {
      mysql_data_seek($rscars, 0);
  $row_rcars = mysql_fetch_assoc($rscars);
  }
?>
  </select>

Share this post


Link to post
Share on other sites
Compare each option's value with the current value. If they are the same, put "selected" inside the option tag
[code]
<option value='1' > abc </option>
<option value='2'  selected> xyz </option>
<option value='3' > mno </option>
[/code]
"xyz" will display in the dropdown

Share this post


Link to post
Share on other sites
Ok  this works - added code in red.  So Barand's suggestion makes sense in comparing the selected with the value of the selected... It works great but my select list has a blank field as a select option ???

<SELECT name="cars" size="1" onchange=cars.submit()>
    <option >Select a car</option>
    <?php
do { 
?>
    <option value="<?php echo $row_rcars['cars']?>[color=red]<?php if ($cars == $row_rcars['cars']) echo "SELECTED"; ?>[/color]"><?php echo $row_cars['car']?></option>
    <?php
} while ($row_cars= mysql_fetch_assoc($rscars));
  $rows = mysql_num_rows($rscars);
  if($rows > 0) {
      mysql_data_seek($rscars, 0);
     $row_rcars = mysql_fetch_assoc($rscars);
  }
?>
  </select>

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.