Current location: Hot Scripts Forums » Programming Languages » PHP » need help with retaining select list values


need help with retaining select list values

Reply
  #1 (permalink)  
Old 03-25-04, 02:48 PM
sky4est sky4est is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
need help with retaining select list values

I have a select list where the option values are populated from a MySQL database. I use this select list to update the rest of the values on the page. I have it updating using a onchange event just fine, but when that happens the value of the option changes back to the default. Any ideas on iterating through the values to write selected on that option?
Thanks,
Sky
Reply With Quote
  #2 (permalink)  
Old 03-25-04, 05:00 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there, there are several ways to do this, but whenever I have to, I tend to use this:

PHP Code:

<?php


foreach ($my_array as $key => $val) {
    
$selected = ($_GET['option_val'] == $key ' selected="selected"' '');
    echo 
'<option value="'.$key.'"'.$selected.'>'.$val.'</option>';
}

?>
Place the above code between <select> and </select>. You have to have an array to hold each option-value/caption pair. Note that $selected determines whether or not the option is to be selected, based on the value $_GET['option_val']. I used $_GET, but it can of course come from the database, your config file, etc.

Hope this gives you some idea.
__________________
Blavv =|
Reply With Quote
Reply

Bookmarks

« user profile | user »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-arranging values in a multi select box.. fluff JavaScript 3 02-24-04 10:45 AM
Putting multiple values from list into one field rush989 PHP 1 02-04-04 04:47 AM
MySQL table problem perleo PHP 9 12-16-03 01:16 PM
Generating external js subcategory list from MySQL using PHP NanoEntity PHP 0 12-15-03 09:08 AM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM


All times are GMT -5. The time now is 10:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.