Perl en Español

  1. Home
  2. Tutoriales
  3. Foro
  4. Artículos
  5. Donativos
  6. Publicidad
 
Índice general » Mundo Perl » Web » CGI con DIV oculto  RESUELTO Responder al tema
Nuevo tema


Página 2 de 2  [ 17 mensajes ]  Ir a página Anterior  1, 2
 
Nota 2011-09-02 02:57 @164

Perlero Nuevo
Registrado: 2009-11-27 05:47 @283
Mensajes: 37
Re: CGI con DIV oculto
Buenos días para que se vea más claro aquí dejo el código completo del script, con las modificaciones que se han ido haciendo a lo largo del hilo.

Syntax: [ Download ] [ Hide ]
Using perl Syntax Highlighting
  1. #!c:/xampp/perl/bin/perl.exe
  2. use strict;
  3. use warnings;
  4. use CGI qw/:standard/;
  5. use DBI;
  6.  
  7.  my $denominacion;
  8.  my $id;
  9.  my $explicacion;
  10.  my $grupo;
  11.  
  12.  
  13. my $dbh=DBI->connect("DBI:mysql:mysql;host=localhost",'user','pass')
  14.         or die "Cannot connect to database:$DBI::errstr";
  15.        
  16. my $sth=$dbh->prepare("SELECT denominacion,grupo,explicacion FROM lineas ORDER BY grupo");
  17. $sth->execute() or die "Cannot execute sth:$DBI::errstr";
  18.  
  19. my $codigo_js = <<EOJ;
  20. function muestra_oculta(id) {
  21.     if (document.getElementById){             // se obtiene el id
  22.         var el = document.getElementById(id); // se define la variable "el" igual a nuestro div
  23.                                               // damos un atributo display:none que oculta el div
  24.         el.style.display = (el.style.display == 'none') ? 'block' : 'none';
  25.     }
  26. }
  27. window.onload = function() {
  28.     // hace que se cargue la función
  29.     // lo que predetermina que div estará oculto hasta llamar a la función nuevamente
  30.     // "contenido_a_mostrar" es el nombre que le dimos al DIV
  31.     muestra_oculta('id');
  32. }
  33. EOJ
  34. ;
  35. print header;
  36. print start_html(
  37.      -title  => 'RECETAS DE COCINA',
  38.      -script => $codigo_js,
  39. );
  40.  
  41.  
  42. while(($id, $denominacion,$grupo,$explicacion)=$sth->fetchrow_array()){
  43.     if ($explicacion eq "") {
  44.         $explicacion="<b>sin datos</b>";
  45.     }
  46.     print
  47.         table(
  48.             Tr({ -align => 'center', -valign => 'top' }, [
  49.                 td([
  50.                     a({ -href => "#", -Onclick => "muestra_oculta('$id')" }, $denominacion),
  51.                     $grupo,
  52.                 ]),
  53.             ]),
  54.         );
  55.      
  56.     print
  57.         div({ id => $id, -style => 'display:none' },
  58.             table(
  59.                 Tr({ -align => 'center', -valign => 'top' }, [
  60.                     td([
  61.                         $explicacion,
  62.                     ]),
  63.                 ]),
  64.             ),
  65.         ),
  66.   };
  67. print end_html;
  68. $dbh->disconnect();


Un saludo
Gonzalo


Nota 2011-09-02 06:31 @313
Avatar de Usuario
Administrador
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España
Mensajes: 10216
Re: CGI con DIV oculto
Esta es mi versión (no probada):
Syntax: [ Download ] [ Hide ]
Using perl Syntax Highlighting
  1. #!c:/xampp/perl/bin/perl.exe
  2. use strict;
  3. use warnings;
  4.  
  5. use CGI qw<:standard>;
  6. use DBI;
  7.  
  8. my $dbh = DBI->connect("DBI:mysql:mysql;host=localhost",'user','pass')
  9.     or die "Cannot connect to database:$DBI::errstr";
  10.  
  11. my $sth = $dbh->prepare("SELECT denominacion,grupo,explicacion FROM lineas ORDER BY grupo");                                                                                    
  12.  
  13. $sth->execute()
  14.     or die "Cannot execute sth:$DBI::errstr";
  15.  
  16. my $codigo_js = <<EOJ;
  17. function muestra_oculta(id) {
  18.     if (document.getElementById){             // se obtiene el id
  19.         var el = document.getElementById(id); // se define la variable "el" igual a nuestro div
  20.                                               // damos un atributo display:none que oculta el div
  21.         el.style.display = (el.style.display == 'none') ? 'block' : 'none';
  22.     }
  23. }
  24. EOJ
  25.  
  26. print
  27.     header,
  28.     start_html(
  29.         -title  => 'RECETAS DE COCINA',
  30.         -script => $codigo_js,
  31.         -onLoad => "muestra_oculta('id')",
  32.     )
  33.     ;
  34.  
  35. while(my($id, $denominacion, $grupo, $explicacion) = $sth->fetchrow_array()) {
  36.  
  37.     $explicacion = b('sin datos') if not $explicacion;
  38.  
  39.     print
  40.         table(
  41.             Tr({ -align => 'center', -valign => 'top' }, [
  42.                 td([
  43.                     a({ -href => "#", -Onclick => "muestra_oculta('$id')" }, $denominacion),
  44.                     $grupo,
  45.                 ]),
  46.             ]),
  47.         ),
  48.         div({ id => $id, -style => 'display:none' },
  49.             table(
  50.                 Tr({ -align => 'center', -valign => 'top' }, [
  51.                     td([
  52.                         $explicacion,
  53.                     ]),
  54.                 ]),
  55.             ),
  56.         )
  57.         ;
  58. }
  59.  
  60. print
  61.     end_html
  62.     ;
  63.  
  64. $dbh->disconnect();

_________________
JF^D Perl programming


Responder al tema  [ 17 mensajes ]  Ir a página Anterior  1, 2

Reglas del Foro
No puedes abrir nuevos temas en este Foro
No puedes responder a temas en este Foro
No puedes editar tus mensajes en este Foro
No puedes borrar tus mensajes en este Foro
No puedes enviar adjuntos en este Foro

Publicidad

Socializa

Síguenos por Twitter

Suscríbete GRATUITAMENTE al Boletín de Perl en Español

Saltar a:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Traducción al español por Huan Manwë para phpbb-es.com
phpBB SEO