Thread Frage zur Automatisierung von Formulareingaben
(7 answers)
Opened by Kuerbis at 2011-08-02 12:39 Code (perl): (dl
)
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 #!/usr/local/bin/perl use warnings; use 5.014; use Mojolicious::Lite; use DBI qw(:sql_types); my $db = 'adressen_db'; my $dbh = DBI->connect( "DBI:SQLite:dbname=$db", "", "", {} ); $dbh->{RaiseError} = 1; $dbh->{PrintError} = 0; $dbh->{AutoCommit} = 1; $dbh->{sqlite_unicode} = 1; $dbh->do("PRAGMA cache_size = 400000"); $dbh->do("PRAGMA synchronous = OFF"); my $table = 'adressen'; #$dbh->do( "CREATE TABLE $table ( vorname TEXT, nachname TEXT, steuernummer TEXT PRIMARY KEY, strasse, nummer INTEGER, ort TEXT, plz INTEGER )" ); get '/namen' => sub { shift->render( 'namen' ); }; get '/steuernummer' => sub { my $self = shift; my $vorname = $self->param('vorname'); my $nachname = $self->param('nachname'); my $sth = $dbh->prepare( "SELECT steuernummer FROM $table WHERE vorname == ? AND nachname == ?" ); $sth->execute( $vorname, $nachname ); my @steuernummern; while ( my $row = $sth->fetchrow_hashref ) { push @steuernummern, $row->{steuernummer}; } $self->stash( vorname => $vorname ); $self->stash( nachname => $nachname ); $self->stash( steuernummern => [ @steuernummern ] ); $self->render( 'steuernummer' ); }; get '/adresse' => sub { my $self = shift; my $steuernummer = $self->param('steuernummer'); my $vorname = $self->param('vorname'); my $nachname = $self->param('nachname'); my $sth = $dbh->prepare( "SELECT strasse, nummer, ort, plz FROM $table WHERE steuernummer == ?" ); $sth->execute( $steuernummer ); my $ref = $sth->fetchall_arrayref; $self->stash( vorname => $vorname ); $self->stash( nachname => $nachname ); $self->stash( steuernummer => $steuernummer ); $self->stash( strasse => '' ); $self->stash( nummer => '' ); $self->stash( ort => '' ); $self->stash( plz => '' ); $self->stash( exists => 0 ); if ( @$ref == 1 and @{$ref->[0]} == 4 ) { my ( $strasse, $nummer, $ort, $plz ) = @{$ref->[0]}; $self->stash( strasse => $strasse ); $self->stash( nummer => $nummer ); $self->stash( ort => $ort ); $self->stash( plz => $plz ); $self->stash( exists => 1 ); } $self->render( 'adresse' ); }; get '/database' => sub { my $self = shift; my $vorname = $self->param('vorname'); my $nachname = $self->param('nachname'); my $steuernummer = $self->param('steuernummer'); my $strasse = $self->param('strasse'); my $nummer = $self->param('nummer'); my $ort = $self->param('ort'); my $plz = $self->param('plz'); my $exists = $self->param('exists'); if ( not $exists ) { my $sth = $dbh->prepare( "INSERT INTO $table ( vorname, nachname, steuernummer, strasse, nummer, ort, plz ) VALUES ( ?, ?, ?, ?, ?, ?, ? )" ); $sth->execute( $vorname, $nachname, $steuernummer, $strasse, $nummer, $ort, $plz ); } $self->render( 'database' ); }; app->start; __DATA__ @@ namen.html.ep <!DOCTYPE html> <html> <head> </head> <body> <br /><br /> <form action="steuernummer"> Vorname : <input type="text" name="vorname" required="required" size="40" maxlength="80" autofocus="autofocus" autocomplete="on" /> <br /><br /> Nachname: <input type="text" name="nachname" required="required" size="40" maxlength="80" autocomplete="on" /> <br /><br /><br /><br /> Weiter : <input type="submit" /> </form> </body> </html> @@ steuernummer.html.ep <!DOCTYPE html> <html> <head> </head> <body> <br /><br /> <form action="adresse"> <input type='hidden' name='vorname' value="<%= $vorname %>"> <input type='hidden' name='nachname' value="<%= $nachname %>"> % if ( @$steuernummern == 0 ) { Steuernummer: <input type="text" name="steuernummer" required="required" size="20" maxlength="20" autofocus="autofocus" /> % } elsif ( @$steuernummern == 1 ) { Steuernummer: <input type="text" name="steuernummer" value="<%= $steuernummern->[0] %>" required="required" size="20" maxlength="20" autofocus="autofocus" /> % } else { Steuernummer: <input type="text" name="steuernummer" required="required" size="20" maxlength="20" list="list_steuernummer" autofocus="autofocus" autocomplete="off" /> <datalist id="list_steuernummer"> % for my $value ( @{$steuernummern} ) { <option value="<%= $value %>" /> % } </datalist> % } <br /><br /><br /><br /> Weiter : <input type="submit" /> </form> </body> </html> @@ adresse.html.ep <!DOCTYPE html> <html> <head> </head> <body> <br /><br /> <h1<%= "$steuernummer" %></h1> <form action="database"> <input type='hidden' name='vorname' value="<%= $vorname %>"> <input type='hidden' name='nachname' value="<%= $nachname %>"> <input type='hidden' name='steuernummer' value="<%= $steuernummer %>"> <input type='hidden' name='exists' value="<%= $exists %>"> Strasse : <input type="text" name="strasse" value="<%= $strasse %>" required="required" size="40" maxlength="80" autofocus="autofocus" autocomplete="on" /> <br /><br /> Nummer : <input type="number" name="nummer" value="<%= $nummer %>" required="required" size="5" maxlength="5" autocomplete="on" /> <br /><br /> Ort : <input type="text" name="ort" value="<%= $ort %>" required="required" size="40" maxlength="80" autocomplete="on" /> <br /><br /> PLZ : <input type="number" name="plz" value="<%= $plz %>" required="required" size="5" maxlength="5" autocomplete="on" /> <br /><br /><br /><br /> Weiter : <input type="submit" /> </form> </body> </html> @@ database.html.ep <!DOCTYPE html> <html> <head> </head> <body> <p>Klicken Sie auf <a href='namen'>weiter</a> um eine weiteren Eintrag hinzuzufügen.</p> </body> </html> Soweit ich es getestet habe, funktioniert es; aber vielleicht kann man hier noch etwas verbessern. |