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

i couldnt get the buttons to work, this is pretty easy with just plain html but with cgi im stuck...


#!/usr/bin/perl -w use strict; use warnings; use CGI::Carp qw/fatalsToBrowser/; use CGI qw(:standard); my $action; print header; print start_html('A Simple Example'), h1('A Simple Example'), start_form(-method=>$method,-action=>$action), "What's your name? ",textfield('name'), p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']), p, #submit, submit(-name=>'hello', -values=>"Hello"), submit(-name=>'hello1', -values=>"Hello1"), end_form, hr; if (submit eq 'hello') { $action = "preview.cgi", } elsif (submit eq 'hello1)' { $action = "post.cgi", } print end_html;

Edited by planetscape - added code tags

( keep:0 edit:23 reap:0 )

Replies are listed 'Best First'.
Re: help with cgi submit buttons
by Joost (Canon) on Jun 29, 2006 at 18:52 UTC
    submit() doesn't return the name or the value of "the submit button". it just generates html.

    you need something like

    if (param('hello') eq 'Hello') { } elsif (param('hello1') eq 'Hello1') { }
    UPDATE: I thought this was "pseudo code". Apparently not.

    Take note that the submitted values can not be read by perl until the form is submitted (i.e. way after you've already generated the HTML and send it to the browser). In other words, $action will always be empty in your code.

    You can fix this in javascript, or use a single action url for the form. or use two forms.

      Thanks.. i modified it a bit and got it to work submit(-name=>'hello', -values=>'hello'),
      submit(-name=>'jello', -values=>'jello'),
      end_form,
      hr;
      if (param('hello') eq 'hello') {
      print "hello";
      }
      if (param('jello') eq 'jello') {
      print "hello1";
      }
        Now Im having problem making the $action variable perform the right action.. Is there something Im missing..
        ##################################### #!/usr/local/bin/perl -wT use CGI qw/:standard/; use strict; use CGI::Carp qw/fatalsToBrowser/; #use Mail::Sendmail; $CGI::POST_MAX = 10000; my $filename = param('filename'); my $action; my $method = "Post"; my $preview = "preview_form.cgi"; my $submit = "createreply.cgi"; my $issues = param('issues'); my $email = param('email'); print header; print start_html('MD Best Practices Forum'), h1('Mechanical Disciplines'), p, h2('Best Practices Executive Forum'), start_form(-method=>$method,-action=>$action), hidden(-name=>'filename', -value=>''), hidden(-name=>'to', -value=>''), hidden(-name=>'from', -value=>''), "What's your beef? ", p, textarea(-name=>'issues', -rows=>'10', -columns=>'60'), p, "Reply from: popup_menu(-name=>'email', -values=>['Select Manager','David','Mark']), submit(-name=>'recommendedby', -value=>'Recommended by'), p, submit(-name=>'submitreply',-value=>'Submit Reply'), submit(-name=>'closefile', -value=>'Close File'), submit(-name=>'preview', -value=>'Preview'), reset(-name=>'reset', -value=>'Reset'), submit(-name=>'submittodept', -value=>'Submit to Dept'), end_form, hr; if (param('preview') eq 'Preview') { print "<P>Your message: <I>$issues</I>\n"; print "<P>Manager: <I>$email</I>\n"; } if (param('submitreply') eq 'Submit Reply') { $action = "createreply.cgi"; }

        20060703 Janitored by Corion: Restored content, added code tags, as per Writeup Formatting Tips

Re: help with cgi submit buttons
by HuckinFappy (Pilgrim) on Jun 29, 2006 at 18:35 UTC
    It would help us help you if you could provide a better explanation of what "doesn't work". Right now, I don't knwo if you get no action, errors in your log, errors to browser, etc.

    What happens when you try your code?

    A reply falls below the community's threshold of quality. You may see it by logging in.