develooper Front page | perl.beginners | Postings from April 2002

CGI, submit buttons, using them as variables.

Thread Next
Tor Hildrum
April 30, 2002 16:16
CGI, submit buttons, using them as variables.
Message ID:
First of all, I would just like to apologize for not sending this to the
CGI-list, but I prefer this list. As this is where I've gotten the best help
in the past. Blame yourself :)

I was reading when I discovered that you could use the -value
argument from submit buttons, and assign them to variables.
submit() will create the query submission button.  Every form
should have one of these.
The first argument (-name) is optional.  You can give the button
a name if you have several submission buttons in your form and
you want to distinguish between them.  The name will also be used
as the user-visible label.  Be aware that a few older browsers
don't deal with this correctly and never send back a value from a
The second argument (-value) is also optional.  This gives the
button a value that will be passed to your script in the query
string.  You can figure out which button was pressed by using
different values for each one:
     $which_one = $query->param('button_name');

I was thinking of ways I could use this, and tried with this idea:

my $q = new CGI;
my $buttonVelgArrangement = undef;

if  ($buttonRegistrer) { FerdigRegistrering(); }
elsif ($buttonVelgArrangement) { Registrer(); }
else { VelgArrangement(); }

sub VelgArrangement {
   print $q->start_html,
           $q->h1("here is a form"),
           ## FORM HERE
           $q->submit(-name=>'velgarrangement', value=>'Velg'),
my $button = \$buttonVelgArrangement;
$button = $q->param('velgarrangement');

When I try to use this method, nothing happens when I press the submit
button. Is this method possible, or am I misunderstanding something?
I was hoping it would jump to Registrer().
Is the problem that when I hit the submit-button, a new CGI call is sent to
the server, and $buttonVelgArrangement is again undef?
Has anyone used this feature of the submit button before (in what way?)?

Any help/info is greatly appreciated.

(I added my test code below)

use Strict;
use DBI;
use CGI qw(:standard);

my $q = new CGI;
my $host = "localhost";
my $user = "tor";
my $password = "password";
my $database = "nuug";
my $table = "arrangement";
my $buttonVelgArrangement = undef;
My $buttonRegistrer = undef;

if ($buttonRegister) { FerdigRegistrering(); }
elsif ($buttonVelgArrangement) { Registrer(); }
else { VelgArrangement(); }

sub VelgArrangement {
my @arrangement;
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host", $user,
$password, {'RaiseError' => 1});
my $sth = $dbh->prepare("select * from $table");
while (my $ref = $sth->fetchrow_hashref()) {
push(@arrangement, $ref->{'kort'});

print   $q->header,
        $q->h1("NUUG Arrangementsregistrering "), hr,
        $q->h3("Velg arrangement: "), popup_menu(-name=>'arrangement',
        $q->submit(-name=>'velgarrangement', -value=>'Velg'),
my $button = \$buttonVelgArrangement;
$button = $q->param('velgarrangement');

sub Registrer { 
print   $q->header,
        $q->textfield(-name=>'navn', -default=>'Ola Nordmann', -size=>35,
        $q->textfield(-name=>'epost', -default=>'Ola\',
-size=>50, -maxlength=>50),
        $q->p,h1('Du valgte: '), $q->param('arrangement'),
        $q->submit(-name=>'registrer', value=>'Registrer'),
My $button = \$buttonRegistrer;
$button = $q->param('registrer');

## I haven't done FerdigRegistrering(), since this submit-experimenting
## didn't work.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About