Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: If statements while in PRINT

by webadept (Pilgrim)
on Mar 14, 2002 at 03:02 UTC ( [id://151588]=note: print w/replies, xml ) Need Help??


in reply to If statements while in PRINT

This might be off base, but it looks like your script is working through a browser input/ form data. If this is so, then the key won't show up if the user doesn't put in an answer for the field, even if you pass that field to your CGI script. In other words if I have a field named "address" and the user doesn't fill in that field then the field address won't be in the parse string.

This function is pretty well known from the O'Riely CGI book (good book to get if you are going to do a lot of this stuff)
sub parse_form_data { local (*FORM_DATA) = @_; local ($request_method, $query_string, @key_value_pairs, $key_value, $key, $value); $request_method = $ENV{'REQUEST_METHOD'}; if ($request_method eq "GET") { $query_string = $ENV{'QUERY_STRING'}; } elsif ($request_method eq "POST") { read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'}); } else { &return_error (500, "Server Error", "Server uses unsupported method"); } @key_value_pairs = split (/&/, $query_string); foreach $key_value (@key_value_pairs) { ($key, $value) = split (/=/, $key_value); $value =~ tr/+/ /; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; if (defined($FORM_DATA{$key})) { $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value); } else { $FORM_DATA{$key} = $value; } } }
Now with that you can write a simple function page like this to see if the variable even exists before using it.
#!/usr/local/bin/perl &parse_form_data (*simple_form); print "Content-type: text/plain", "\n\n"; $address = $simple_form{'address'}; if ($address) { print "Your Address is ", $simple_form{'address'}, ".", "\n"; print "Please visit this Web server again!", "\n"; } else { print "You did not enter a address.", "\n"; print "But, you are welcome to visit this Web server again!", "\n" +; } exit(0);
Don't give me credit for the code, again its paraphrased from the O'Riely CGI book, but as you can see it takes care of the problem on if you got the information or not.

Hope this helps

Glenn H.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://151588]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found