Jump to content

HELP with SELECTED attridute


imimin

Recommended Posts

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

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

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
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.