Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 drranch

drranch
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 26 August 2006 - 05:07 AM

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>

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 26 August 2006 - 10:05 AM

Compare each option's value with the current value. If they are the same, put "selected" inside the option tag
<option value='1' > abc </option>
<option value='2'  selected> xyz </option>
<option value='3' > mno </option>
"xyz" will display in the dropdown
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 drranch

drranch
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 03 September 2006 - 12:37 AM

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']?><?php if ($cars == $row_rcars['cars']) echo "SELECTED"; ?>"><?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>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users