imimin Posted May 16, 2009 Share Posted May 16, 2009 I have a SELECT statement (DROPDOWN list) that has 7 OPTIONS. Is it possible to use the SELECTED attribute in such a way that I can pass a variable (such as $item_selected_color) and have have what ever value is passed let that be my SELECTED item? EXAMPLE OF WHAT I NEED TO DO: <SELECT NAME="custom20" <OPTION VALUE='$item_selected_color' SELECTED>$item_selected_color</OPTION> <OPTION VALUE='Color: RED Hibiscus'>RED Hibiscus</OPTION> <OPTION VALUE='Color: Dark Blue Hibiscus'>Dark Blue Hibiscus</OPTION> <OPTION VALUE='Color: Turquoise Hibiscus'>Turquoise Hibiscus</OPTION> <OPTION VALUE='Color: Purple Hibiscus'>Purple Hibiscus</OPTION> <OPTION VALUE='Color: White'>White</OPTION> <OPTION VALUE='Color: American Flag'>American Flag</OPTION> <OPTION VALUE='Color: Hundred Dollar Bill'>Hundred Dollar Bill</OPTION> </SELECT> The code above I just made up (it probably makes no sense). WHAT I NEED FOR IT TO DO is what ever value is passed to '$item_selected_color'(RED Hibiscus, Dark Blue Hibiscus, etc) needs to be the selected item in my DROPDOWN list. Thank you! Link to comment https://forums.phpfreaks.com/topic/158390-help-with-selected-attridute/ Share on other sites More sharing options...
wildteen88 Posted May 16, 2009 Share Posted May 16, 2009 Yes it is possible. Here is an example // ,list all items for the menu $items = array('Red Hibiscus', 'Blue Hibiscus', 'Purple Hibiscus'); // start form/menu echo '<from action="'.$_SERVER['PHP_SELF'].'" method="post"> <select name="color">'; // generate the list items foreach($items as $item) { // this part decides whether the current item will be selected $selected = (isset($_POST['color']) && $_POST['color'] == $item) ? ' selected="selected"' : ''; echo '<option value="'.$item.'"'.$selected.'>'.$item.'</option>'; } // end menu/form echo '</select> <input type="submit" name="submit" value="Choose" /> </form>'; Link to comment https://forums.phpfreaks.com/topic/158390-help-with-selected-attridute/#findComment-835312 Share on other sites More sharing options...
imimin Posted May 16, 2009 Author Share Posted May 16, 2009 THANK YOU! The following JAVASCRIPT (which I would like to set up to work exclusively with PHP) and HTML/php not only produce a dropdown list but also display an image of my selected item. The problem is when I pass a specific item to '$item_selected_color' on my first OPTION line, it does not select it. Would you be able to tell me how to modify my SELECT code to make the SELECTED attribute SELECTED for the passed '$item_selected_color' variable? Or better yet, how to set this whole thing up to work exclusively with PHP? Thank You! <SCRIPT LANGUAGE="javascript"> GarmentColorArray = new Array (; GarmentColorArray[1] = "POJ00215.jpg"; GarmentColorArray[2] = "POJ00216.jpg"; GarmentColorArray[3] = "POJ00217.jpg"; GarmentColorArray[4] = "POJ00218.jpg"; GarmentColorArray[5] = "POJ00219.jpg"; GarmentColorArray[6] = "POJ00220.jpg"; GarmentColorArray[7] = "POJ00221.jpg"; GarmentColorArray[8] = "POJ00222.jpg"; function GarmentColorImageSwap(imgBase,imgIndex) { Index = imgIndex + 1; document.GarmentColorImage.src = '/images/thumbs/' + GarmentColorArray[index]; } </SCRIPT> <!-- Garment Color = custom20--> <SELECT NAME="custom20" ONCHANGE="GarmentColorImageSwap('GarmentColorImage', this.selectedIndex)" SIZE="1"> <?php $item_selected_style = $_GET['item_selected_color']; $item_prod_name = $_GET['item_selected_size']; echo " <OPTION VALUE='$item_selected_color'>$item_selected_color</OPTION> <OPTION VALUE='Color: RED Hibiscus'>RED Hibiscus</OPTION> <OPTION VALUE='Color: Dark Blue Hibiscus'>Dark Blue Hibiscus</OPTION> <OPTION VALUE='Color: Turquoise Hibiscus'>Turquoise Hibiscus</OPTION> <OPTION VALUE='Color: Purple Hibiscus'>Purple Hibiscus</OPTION> <OPTION VALUE='Color: White'>White</OPTION> <OPTION VALUE='Color: American Flag'>American Flag</OPTION> <OPTION VALUE='Color: Hundred Dollar Bill'>Hundred Dollar Bill</OPTION> </SELECT>" ?> Link to comment https://forums.phpfreaks.com/topic/158390-help-with-selected-attridute/#findComment-835321 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.