Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Trouble getting CGI parameters

by gitarwmn (Beadle)
on Apr 30, 2006 at 22:37 UTC ( #546609=perlquestion: print w/replies, xml ) Need Help??

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

I'm dabbling with a CGI login page for the first time so I can learn a bit about CGI and I am using a simple flat file to write data to for the moment. When I open the file and try to print the users information to the flat text file it doesn't seem to print the information only the variable divider. Can someone tell me what I'm doing wrong?

#!c:/perl/bin/Perl.exe use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Fcntl qw(:flock :seek); use CGI; use CGI::Cookie; use XML::Simple; use strict; my $passfile = "userfile.txt"; print header; print start_html("New User"); my $q = CGI->new; my $self = $q->url; # the path to this script my $username = $q->param('username') ; my $first = $q->param('first') || ''; my $last = $q->param('last') || ''; my $pass1 = $q->param('pass1') || ''; my $pass2 = $q->param('pass2') || ''; #new user signup page print <<End_of_HTML; </head><body bgcolor="#ffffff"> <form action="http://localhost/newuser.cgi?action=newuser?first?last" +method="post"> <h2>New User Form</h2> First name? <input type="text" name="first" value=""> <br />Last name? <input type="text" name="last" value=""> <br />Username (2-12 characters)? <input type="text" name="username" +value=""> <br />Password <input type="password" name="pass1"> <br />Password (verify) <input type="password" name="pass2"> <input type="hidden" name="action" value="validate_newuser"> <br /> <br /> <input type="submit" value="Go!"> </form> <br /> <br /> <i>Thank you for signing up. </i> End_of_HTML open(FILE, ">>$passfile") or die "can't open password file"; print FILE "$first:$last:$username"; close(FILE);

2006-05-02 Retitled by GrandFather, as per Monastery guidelines
Original title: 'Wondering why this doesn't work'

Replies are listed 'Best First'.
Re: Trouble getting CGI parameters
by japhy (Canon) on May 01, 2006 at 00:00 UTC
    Your program does two things. It prints HTML output (a form you need to fill in) and then it appends to a data file. But you should only be appending to the data file after the form has been submitted. And your FORM tag has a very weird URL: "http://localhost/newuser.cgi?action=newuser?first?last" doesn't make a lot of sense. You probably just want "http://localhost/newuser.cgi".

    Here is a simple program:

    #!/usr/bin/perl use CGI; use CGI::Carp qw( fatalsToBrowser ); use strict; use warnings; my $query = CGI->new; # if we got here from a form being submitted, # do the adduser() stuff adduser($query) if $query->param; print << "END_HTML"; <html> <body> <form method="post" action="thisprogram.cgi"> Username: <input type="text" name="user"><br> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> Password: <input type="password" name="pass"><br> <input type="submit" value=" Add User "> </form> </body> </html> END_HTML sub add_user { my ($q) = @_; open PASSWD, ">> $passwd_file" or die "can't append to $passwd_file: $!"; print PASSWD join(":" => $q->param('first'), $q->param('last'), $q->param('user'), $q->para +m('pass') ), "\n"; close PASSWD; }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Trouble getting CGI parameters
by PodMaster (Abbot) on Apr 30, 2006 at 23:15 UTC
    it doesn't seem to print the information only the variable divider. Can someone tell me what I'm doing wrong?
    If print FILE "$first:$last:$username"; only produces "::" in the file, that means that $first, $last and $username are EMPTY. Why they're empty? It could be because you're mixing function/object interfaces. You should examine your object with Data::Dumper (use Data::Dumper;print Dumper($q);).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Trouble getting CGI parameters
by GrandFather (Saint) on Apr 30, 2006 at 22:56 UTC

    How about reducing the code to just show your problem and to eliminate anything external that isn't pertinent to the problem.

    For a start, check that the parameter values are as you expect. If they are, place $q->param('...') with a constant string. That makes it easier for use to reproduce your issue. If the parameters are not what you expect, then you have learned something :).

    For test purposes use print rather than printing to a file and show us what you get and what you expect.

    Remove any use lines that you don't absolutly need (except use strict; use warnings;) to show the problem.

    Reduce any HTML to the minimum required to show the problem.

    DWIM is Perl's answer to Gödel

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://546609]
Approved by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2022-12-05 15:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found