drranch Posted August 26, 2006 Share Posted August 26, 2006 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> <?phpdo { ?> <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> Quote Link to comment Share on other sites More sharing options...
Barand Posted August 26, 2006 Share Posted August 26, 2006 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 Quote Link to comment Share on other sites More sharing options...
drranch Posted September 3, 2006 Author Share Posted September 3, 2006 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> <?phpdo { ?> <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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.