1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# eins.cgi use CGI; use JSON; my @array = ( [ 1, 'eins', '12/11/10', 'sn' ], [ 2, 'zwei', '11/12/09', 'cp' ], [ 3, 'drei', '04/09/11', 'cp' ] ); my $radio_values, encode_json /@array; my $cgi = new CGI(); print $cgi->header(); print $cgi->start_html(); print $cgi->startform( -action => 'zwei.cgi', -method => 'POST' ); print $cgi->radio_group( -name => 'choice', -values => [ @$radio_values ], -linebreak => 'true' ); print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# zwei.cgi use CGI; my $cgi = new CGI(); my $choice = $cgi->param('choice'); print $cgi->header(); print $cgi->start_html(); print $cgi->startform( -action => 'drei.cgi', -method => 'POST' ); print $cgi->hidden( -name => 'choice', -value => $choice ); print $cgi->h2( "Rating: $choice" ); print $cgi->popup_menu( -name => 'rating', -values => [ '+++', '++', '+', '0', '-', '--', '---' ], -default => '0' ); print $cgi->br(); print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# drei.cgi use CGI; use JSON; my $cgi = new CGI(); my $choice = $cgi->param('choice'); my $rating = $cgi->param('rating'); my $a_ref = decode_json $choice; my $name = $a_ref->[1]; my $table = $a_ref->[-1]; print $cgi->header(); print $cgi->start_html(); print $cgi->p( "Table: $table<br />Name: $name<br />RATING: $rating" ); print $cgi->end_html();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
... my $step = $cgi->param('step') || 0; my @functions = ( \&eins, \&zwei, &\drei ); { no warnings; $step = int $step; } $step = 0 if $step > 2; my $function = $functions[$step] or die "invalid $step"; my $hidden = $cgi->hidden( -name => 'step', -value => $step+1 ); $function->(); sub eins { # code von eins.cgi ... print $hidden; } sub zwei { # code von zwei.cgi ... print $hidden; } ...
2011-05-15T16:55:22 GwenDragonJa, der Schreiber des CGI muss eben darauf achten, welchen Werte welches CGI in der Ablaufkette zugeordnet ist.
my $step = (defined $cgi->param('step') ? $cgi->param('step') : 1);
2011-05-15T17:33:33 pqaber wenn du das mit dem defined schöner findest und besser zu verstehen, bitte...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#!/usr/local/bin/perl use warnings; use 5.012; use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use JSON; my $ref = [ [ 1, 'eins', '12/11/10', 'sn' ], [ 2, 'zwei', '11/12/09', 'cp' ], [ 3, 'drei', '04/09/11', 'cp' ], ]; my @radio_values; for my $el ( @$ref ) { push @radio_values, encode_json $el; } my $cgi = new CGI(); my $step = $cgi->param('step') // 0; my @functions = ( \&eins, \&zwei, \&drei ); my $function = $functions[$step] or die "invalid $step"; my $hidden = $cgi->hidden( -name => 'step', -value => $step+1 ); $function->(); sub eins { print $cgi->header(); print $cgi->start_html(); print $cgi->startform(); print $cgi->radio_group( -name => 'choice', -values => [ @radio_values ], -linebreak => 'true' ); print $hidden; print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html(); } sub zwei { my $choice = $cgi->param('choice'); print $cgi->header(); print $cgi->start_html(); print $cgi->startform(); print $cgi->h2( "Rating: $choice" ); print $cgi->popup_menu( -name => 'rating', -values => [ '+++', '++', '+', '0', '-', '--', '---' ], -default => '0' ); print $cgi->br(); print $cgi->hidden( -name => 'choice', -value => $choice ); print $hidden; print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html(); } sub drei { my $choice = $cgi->param('choice'); my $rating = $cgi->param('rating'); my $a_ref = decode_json $choice; my $name = $a_ref->[1]; my $table = $a_ref->[-1]; print $cgi->header(); print $cgi->start_html(); print $cgi->p( "Table: $table<br />Name: $name<br />RATING: $rating" ); print $cgi->end_html(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#!/usr/local/bin/perl use warnings; use 5.012; use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $cgi = new CGI(); my $step = $cgi->param('step') // 0; my @functions = ( \&eins, \&zwei, \&drei ); my $function = $functions[$step] or die "invalid $step"; $function->(); sub eins { print $cgi->header(); print $cgi->start_html(); print $cgi->p('eins'); print $cgi->startform(); print $cgi->hidden( -name => 'step', -value => 1 ); print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html(); } sub zwei { print $cgi->header(); print $cgi->start_html(); print $cgi->p('zwei'); print $cgi->startform(); print $cgi->hidden( -name => 'step', -value => 2 ); print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html(); } sub drei { print $cgi->header(); print $cgi->start_html(); print $cgi->p('drei'); print $cgi->startform(); print $cgi->hidden( -name => 'step', -value => 0 ); print $cgi->submit( 'OK' ); print $cgi->endform(); print $cgi->end_html(); }
QuoteSteht auch unter CGI bei CREATING A HIDDEN_FIELDNote, that just like all the other form elements, the value of a hidden field is "sticky". If you want to replace a hidden field with some other values after the script has been called once you'll have to do it manually
print $cgi->hidden( -name => 'step', -value => 1, -override => 1);
$cgi->param( -name => 'step', -value => 1);
2011-05-16T11:52:32 GwenDragonÜberschreiben eines hidden-Feldes:
2011-05-15T15:00:49 KuerbisHallo,
könnte ich diese drei Skripte in einem einzigen unterbringen oder braucht es hier diese drei Skripte?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if($cgi->param){ if($cgi->param('senden')){ # header, ganze Seite } elsif($cgi->param('x_senden')){ # header, json } else{ # unbekannter Parameter } } else{ # ganze Seite für die Response, wenn keine Parameter }