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

i am trying to make a cgi script for muliple users, where you specify the user like this: "/cgi-bin/guestbook.pl?user=foo" now, sometimes people arent so smart, and might leave off the user part. so when that happens i want to have a form show with a drop down box and a submit button, like so:
sub no_user { my $query = shift; print $query->h1( "Sorry, you must specify a username." ); print $query->startform(); print $query->popup_menu( -name=>'user', -values=>['main','foo','bar'], -default=>['main'] ); print $query->submit; }
how do i make that add '?user=foo' to the url?

Replies are listed 'Best First'.
(Ovid) Re: Forms and CGIs
by Ovid (Cardinal) on Nov 13, 2000 at 23:33 UTC
    Why do you need to specify that "?user=foo" be in the URL? If you are using a GET request, it is automatically added to the query string. If you are using a POST request, it is added to the entity-body of the request and CGI.pm will read it from STDIN. Either way, you get your data. Also, don't forget your print $query->endform; tag.

    The only problem will occur if you are using the POST method with data attached to the URL, which gets read into the $ENV{QUERY_STRING}, but won't be part of your params. You need to hack CGI.pm to allow that (but you shouldn't be mixing GET and POST methods anyway :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

Re: Forms and CGIs
by wardk (Deacon) on Nov 14, 2000 at 00:01 UTC

    By providing a nice form with a drop-down, you can avoid worrying about the user=foo string on the URL. This has a minor advantage that a typical end-user may not be curious about the user=foo and play around with it (by typing other id's).

    I would tend to avoid having an end user worry at all about query_string data, i.e. having them fill in form variables in the browser's "Location" entry field, and make them use widgets you supply, like in this case, a simple popup menu.

Re: Forms and CGIs
by Fastolfe (Vicar) on Nov 13, 2000 at 23:26 UTC
    You can examine $ENV{QUERY_STRING}, $ENV{UNPARSED_QUERY_STRING}, or just look at $query->keywords, which will split each argument to your script and return an array.
    http://example.com/cgi-bin/your-script.pl?username my $username = $query->keywords;
Re: Forms and CGIs
by cianoz (Friar) on Nov 14, 2000 at 22:14 UTC
    change
    print $query->startform(); in print $query->startform(-method=>'GET');