Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Insertar el selected

Estas en el tema de Insertar el selected en el foro de PHP en Foros del Web. Buenas a todos, estoy haciendo una resistencia la cual con un cuadro combinado tiene que cambiar la franja izquierda de la resistencia y eso funciona ...
  #1 (permalink)  
Antiguo 06/01/2014, 10:22
 
Fecha de Ingreso: enero-2014
Mensajes: 2
Antigüedad: 10 años, 3 meses
Puntos: 0
Insertar el selected

Buenas a todos, estoy haciendo una resistencia la cual con un cuadro combinado tiene que cambiar la franja izquierda de la resistencia y eso funciona pero ahora en el cuadro combinado se tiene que quedar fijo el último color seleccionado y no consigo utilizar el selected para realizar dicha función.

Código PHP:
<?php
$colors 
= array(
    
=> "Negre",
    
=> "Marró",
    
=> "Vermell",
    
=> "Carbassa",
    
=> "Groc",
    
=> "Verd",
    
=> "Blau",
    
=> "Violeta",
    
=> "Gris",
    
=> "Blanc");

function 
opcio1($szColorNombre$szColor){
            echo 
"<option selected='selected' value=\"$szColorNombre\">$szColor</option>\n";
    }
?>
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type">
        <title>Canvi d'un color d'un resistor bàsic</title>
    </head>
    <body>
        <br>
        <form method='POST' action='ex03f.php'>
            <table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="2">
              <tbody>
                <tr>
                  <td>
                    <IMG SRC='imatges/resleft.png' WIDTH=127 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>
                    <?php
                    $barColor 
= array("rBlack.png","rBrown.png","rRed.png","rOrange.png","rYellow.png",
                                   
"rGreen.png","rBlue.png","rViolet.png","rGray.png","rWhite.png");
                                   print 
"<IMG SRC='imatges/".$barColor[$_REQUEST["tensSelect"]]."' NAME='tens' WIDTH=21 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>";
                    
?>
                    <IMG SRC='imatges/rBrown.png' NAME='ones' WIDTH=21 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>
                    <IMG SRC='imatges/rRed.png' NAME='mult' WIDTH=21 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>    
                    <IMG SRC='imatges/spacer.png' WIDTH=17 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>                        
                    <IMG SRC='imatges/rGold.png' NAME='tol' WIDTH=21 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>      
                    <IMG SRC='imatges/resright.png' WIDTH=127 HEIGHT=182 ALIGN=left HSPACE=0 BORDER=0>                    
                 </td>
                </tr>
                <tr>
                 <td>
                    <br>
                    <br>
                    <select name="tensSelect" onchange="submit()">
                        <?php
                          
foreach($colors as $clave => $valor){
                                
opcio1($clave$valor);
                          }
                        
?>
                    </select>
                 </td>
                </tr>
              </tbody>
            </table>
          </form>
        <br>
        <br>
    </body>
</html>
  #2 (permalink)  
Antiguo 06/01/2014, 17:10
Avatar de patriciomase  
Fecha de Ingreso: abril-2012
Ubicación: Mar del Plata
Mensajes: 323
Antigüedad: 12 años
Puntos: 34
Respuesta: Insertar el selected

Si lo que queres es que se "acuerde" cual color enviaste te falta un if

Código PHP:
Ver original
  1. function opcio1($szColorNombre, $szColor){
  2.  
  3.     $selected = "";
  4.     if($szColorNombre == $_POST["tensSelect"])
  5.         $selected = "selected";
  6.  
  7.     echo "<option " . $selected . " value=\"$szColorNombre\">$szColor</option>\n";
  8.     }
  #3 (permalink)  
Antiguo 07/01/2014, 08:47
 
Fecha de Ingreso: enero-2014
Mensajes: 2
Antigüedad: 10 años, 3 meses
Puntos: 0
Respuesta: Insertar el selected

Muchas gracias por tu ayuda!!

Etiquetas: html, select, selected
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:20.