Jump to content

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


drranch

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>
Link to comment
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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.