Ver Mensaje Individual
  #19 (permalink)  
Antiguo 10/12/2009, 00:21
Avatar de masterojitos
masterojitos
 
Fecha de Ingreso: julio-2008
Ubicación: Lima Callao Chucuito
Mensajes: 1.931
Antigüedad: 15 años, 9 meses
Puntos: 105
Respuesta: Pasar array a SELECT con PHP

Segun entiendo.... al hacer submit deseas que se quede seleccionado el valor seleccionado anteriormente.......... y ps eso se hace asi......

Código html:
Ver original
  1. <?php
  2. session_start();
  3. //$_SESSION['name'] = "usuario1";
  4. if(empty($_SESSION['name'])) header("Location:login.html");
  5. $name = $_SESSION['name'];
  6. ?>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10. <title>Documento sin t&iacute;tulo</title>
  11. <script type="text/javascript">
  12. var usuario1 = new Array("Seleccione la fruta","banana","pera","manzana","sandia");
  13. var usuario2 = new Array("Seleccione la fruta","manzana","naranja");
  14. var usuario3 = new Array("Seleccione la fruta","banana","toronja","uva");
  15.  
  16. function setSelect(){
  17.     fruta = '<select name="fruta" id="fruta">';
  18.     for(a = 0; a < <?php echo $name; ?>.length; a++){
  19.         selected = (<?php echo $name; ?>[a] == <?php echo $_REQUEST['fruta']; ?>) ? "selected='selected'" : "";
  20.         fruta+= '<option value="' + <?php echo $name; ?>[a] + '" ' + selected + '>' + <?php echo $name; ?>[a] + '</option>';
  21.     }
  22.     fruta+= '</select>';
  23.     document.getElementById('select').innerHTML = fruta;
  24. }
  25. </head>
  26. <body onload="setSelect()">
  27. <form id="form1" action="" method="post">
  28.     <div id="select"></div>
  29. </form>
  30. </body>
  31. </html>

agregue una validacion comparando el valor recorrido con el que viene del select fruta al hacer submit.

espero te sirva....

Suerte.
__________________
Atte. MasterOjitos :ojotes:
Todo sobre Programación Web
Las ultimas tendencias en Efectos y Recursos Web: MasterOjitos Blog