Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I beg for a favour again. I am learning cgi, and have created a few forms at this stage, and everything seems alright, but I've hit a problem. I know its possible to have two buttons in a cgi form

$co->submit(-name=>'edit', -value=>'edit'), $co->submit(-name=>'preview', -value=>'preview'),
At the top of my script I have tried this, as suggested on some nodes:
my $name=>$co->param('name'); if ($name eq 'edit'){ $string="http://mywebserver/cgi-bin/edit.cgi"; } elsif($name eq 'preview'){ $string="http://mywebserver/cgi-bin/preview.cgi"; }
And where I specify the start of the form I use this:
$co->start_form(-method=>'POST', -action=>"$string"),
The two buttons appear alright in the form, I was hoping that they would each call the different scripts as outlined in the if/elsif statement, but they webpage just seems to refresh. If anybody could point me in the right direction, I would appreciate it a lot.

Thanks,
Jonathan

Replies are listed 'Best First'.
Re: Two cgi buttons in the same form
by inman (Curate) on Mar 26, 2004 at 11:55 UTC
    From an HTML point of view you need to give each button a name. The name of the button is then passed as a parameter to your cgi app in the normal way. E.g.

    <input type="submit" name="go" value="Go!"/> <input type="submit" name="else" value="Something else!"/>

    update

    Just to clarify the processing but the same CGI application (as specified in the From action) would receive the information from both buttons. It would be up to the application to take a different course of action depending on which button was pressed.

    If you really want to submit to different forms then you will have to resort to javascript to change the action of the form before you submit.

Re: Two cgi buttons in the same form
by Happy-the-monk (Canon) on Mar 26, 2004 at 12:32 UTC

    That's allright to change the action attribute in the form of the page you are getting after submitting the form.

    The idea you're having looks a bit like this.

    Cheers, Sören

Re: Two cgi buttons in the same form
by Plankton (Vicar) on Mar 26, 2004 at 16:21 UTC
    Hi Jonathan, Here's a simple example that I hopes will help you out ...
    use strict; use CGI; use CGI::Carp qw /fatalsToBrowser/; my $out = new CGI; my $button1 = $out->param ( 'button1' ); my $button2 = $out->param ( 'button2' ); if ( $button1 ) { my $url = "/~Plankton/button1.html"; print $out->redirect ( -URL => $url ); } elsif ( $button2 ) { my $url = "/~Plankton/button2.html"; print $out->redirect ( -URL => $url ); } else { print $out->header( 'text/html' ); print <<HTML; <FORM action="twobuttons.pl" method="post"> <input type='submit' name='button1' value='button1'> <input type='submit' name='button2' value='button2'> </FORM> </BODY> </HTML> HTML } # end if button1

    Plankton: 1% Evil, 99% Hot Gas.
Re: Two cgi buttons in the same form
by maa (Pilgrim) on Mar 26, 2004 at 11:46 UTC

    From the documentation:

    print $query->defaults('button_label');

    I'm guessing that your problem is that your form should probably only have one button where type=submit... unless you'er doing something odd.

    HTH - mark