Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with querystring 1

Status
Not open for further replies.

coders4hire

IS-IT--Management
Dec 31, 2003
22
US
I've got the start on retrieving a querystring. I simply want to make a completion page where a status code is passed so that the text message can be adjusted.

Examples:
thanks.cgi?status=0
print "The service is currently unavailable"

thanks.cgi?status=1
print "The request was successful"

thanks.cgi?status=2
print "The request was unsuccessful"

I have no experience with perl, though. I've made the page, set CHMOD to 755 and I can get the status=0 back. I'm just having problems actually returning text for each status code. Here's what I have:

#------------- begin code ---------------------
#!/bin/perl
print "Content-type:text/html\n\n";
while (($key,$value)=each(%ENV))
{
print "<h3><I>$value</i></h3><br>"
if $key eq "QUERY_STRING";
}
#-------------- finish -------------------------

On a similar note, if I want to incorporate this message into an entire page, do I need to transform the contents of that page totally into 'print' commands in the Perl script? That seems a little inefficient.

Thanks!

Doug
 
dump what you have and start using CGI.pm to parse form data and query string data:

Code:
#!/bin/perl
use strict;
use CGI qw/:standard/;
my %status = (
0 => 'The service is currently unavailable',
1 => 'The request was successful',
2 => 'The request was unsuccessful',
);
my $message = param('status');
print header;
print start_html;
if (exists $status{$message}) {
   print qq~<h3><I>$status{$message}</i></h3>~;
}
else {
   print qq~<h3><I>Invalid entry</i></h3>~; 
}
print end_html;

also get used to the idea of using "use strict" it will make your life much easier if you continue with perl.

side note: you generally don't need a <br> tag after a closing </h> tag since <h> is block level and has an implict break when it's closed.
 
or even
Code:
#!/bin/perl
use strict;
use CGI qw/:standard/;
my %status = (
  0 => 'The service is currently unavailable',
  1 => 'The request was successful',
  2 => 'The request was unsuccessful',
);
print header, start_html,
  h3( i( $status{param('status')} || 'Invalid Entry' ) ),
  end_html;

The two schools of thought seem to be (i) don't muck up your namespace with lots of imported functions .v. (ii) ensure valid HTML tag nesting and save typing by using CGI shortcut functions. I lean to the latter.

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Thanks! And I'll follow up with a dumb question regarding the second half of my question. My page has a header, left menu, etc. on it. Do I need to include all of those in this script? Can I do includes? (Perhaps header, start_html, and end_html are includes... but I don't know where to declare or reference them).



Doug
 
I don't think there's a consensus on this. I tend to program it all in perl (although I move the common stuff into modules). HTML::Template lets you write almost entirely in html and just import variables. A halfway house would be something like
Code:
print header,
  `cat /var/[URL unfurl="true"]www/default_template.html`;[/URL]
   div( {
         -style=>'position:absolute;top:250px;left:120px;'
       },
     h1( 'Variable content' ),
     ....
   ),
   end_html;[code]

Positioning the div with 'absolute' lets you lay out all your standard chrome, menus, etc leaving a space blank for your variable content.

hth,

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought.  Debugging had to be discovered.  I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
         --Maurice Wilkes
 
I'd agree with the recommendation for HTML::Template[. It's pure-Perl so it's easy to install (even without shell access), it's quite easy to use after a little practice and makes for wonderful seperation of logic and presentation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top