Hello!

Is there any way to make a "<select>" keep it's selected <option> when the pages is beeing reloaded as:
"<form action="<? echo ($_SERVER['PHP_SELF']); ?>"
method="post"> ".

My code so far:

<select size="1" name="modell">
         <?php for($k=0;$k<$10;$k++){
         echo "<option value='".$modell[$k]. "'>".$modell[$k]."</option>";
		 }//end of for
?>

Somehow I would like the selected value to be <option selected="the selected value">the selected value</option>

I tried:

if ($modell[$k]==$selected_modell[$k]){
	        echo "<option value selected='".$modell[$k]. "'>".$modell[$k]."</option>";

but it didn't work...

Anyone who knows how to do this? Or know of some good page where I can read about it?

Best regards
/Emil Hansson

    try something along these lines, there may be parse errors cuz i couldn't test, but you should get the idea

    <select size="1" name="modell">
    <?
    for($k=0;$k<10;$k++){
    ?>
    <option value="<? echo $modell[$k]; ?>" <? if ($modell[$k]){ echo "selected"; } else {} ?>><? echo $modell[$k]; ?></option>
    <?
    }
    ?>
    </select>	
    

      thanks but $modell[$k] is always true. I have made that array from a database.

      while ($row = mysql_fetch_array($result)){
        $modell[$x]=$row["modell"];
        $x++;
      }
      

        play around with this, this is what i gave someone else asking the same question

        <select name="eye">
        <option value="Blue" <? if ($row_user['eye'] == "Blue"){ echo "selected";} else {} ?>>Blue</option>
        <option value="Brown" <? if ($row_user['eye'] == "Brown"){ echo "selected";} else {} ?>>Brown</option>
        <option value="Green" <? if ($row_user['eye'] == "Green"){ echo "selected";} else {} ?>>Green</option>
        </select>
        
          <select name="selected_eye">
          <option value="Blue" <? if ($row_user['eye'] == $selected_eye){ echo "selected";} else {} ?>>Blue</option>
          <option value="Brown" <? if ($row_user['eye'] == $selected_eye){ echo "selected";} else {} ?>>Brown</option>
          <option value="Green" <? if ($row_user['eye'] == $selected_eye){ echo "selected";} else {} ?>>Green</option>
          </select>
          

          Why doesn't this work? I can't see what's wrong.

          the select variable is now named "selected_eye" it's value should be the option I select right?

          Thanks for helping me out. I searched for similar questions in this forum but I didn't find anything that solved my problem.

            anyway, it would be a good idea to output select-boxes with the help of a function. so you wouldn't have to put all these ugly if statements into every line of your HTML code.

            try something like this (have no working PHP environment here, so there may be an error in it... g)

            function htmlSelect( $name, $keyValues, $selectKey )
            {
            	$s = "<select name=\"{$name}\">\n";
            	while (list($key, $value) = each($selectValues) )
            	{
            		$selected = '';
            		if( $key == $selectKey )
            		{
            			$selected = ' selected';
            		}
            		$s .= "<option value=\"{$key}\"{$selected}>{$value}</option>\n";
            
            }	
            $s .= "</select>\n";
            
            return $s;
            }
            
            
            
            $selectValues = { 'blue' => 'Blue',
            		        'green' => 'Green',
            		        'gray' => 'Gray' };
            echo htmlSelect( 'eyeColor', $selectValues, $_POST['eyeColor'] );

            With that function you just have to use the array and the function call in your HTML code

            hope that helps,
            tREXX

              Write a Reply...