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

CGI, submit buttons, using them as variables.

Thread Next
From:
Tor Hildrum
Date:
April 30, 2002 16:16
Subject:
CGI, submit buttons, using them as variables.
Message ID:
B8F4F29A.98B0%torhildr@mac.com
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 CGI.pm when I discovered that you could use the -value
argument from submit buttons, and assign them to variables.
From CGI.pm:
###########
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
button.
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'),
           $q->end_html;
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.

Tor 
(I added my test code below)

-- START FULL EXAMPLE CODE
#!/usr/bin/perl
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");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
push(@arrangement, $ref->{'kort'});
}

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

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

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

--END OF CODE


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About