in reply to HTTP headers and redirection

The three if-statements you have makes it possible that all three applies;
if ($Salesperson eq "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } if ($Sales == "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } if ($Rate == "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; }
It is better if the two later (for $Sales and $Rate) is used with elsif instead of if. In that case the script won't process any further elsif or else if any of them applies.

Replies are listed 'Best First'.
Re^2: HTTP headers and redirection
by student (Novice) on Nov 11, 2004 at 04:17 UTC
    Thanks, But please see what I sent to jzed here and provide any input if you could >> Not sure exactly what you mean... I edited my script like this and it's still not working. If I move my print "Content-type: text/html\n\n"; things will not work correctly. Also I have no idea how to use CGI.pm's header and redirect methods. This book doesn't go into depth nearly enough to complete these assignments. This is frustrating for me Any help? I am a novice. This is my first Perl Class. Thanks,
    #!c:\phpdev\perl\bin\perl.exe #c12ex3.cgi - Location Header print "Content-type: text/html\n\n"; use CGI qw(:standard -debug); #prevent Perl from creating undeclared variables use strict; #declare variables my ($Salesperson, $Sales, $Size, $Rate, $Percentage, @records, @errors +, $errors, $bonus); #assign input items to variables $Salesperson = param('Salesperson'); $Sales = param('Sales'); $Rate = param('Rate'); $Percentage = param('Percentage'); #calculate bonus rates $bonus = $Rate * $Sales; if ($Salesperson eq "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } elsif ($Sales == "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } elsif ($Rate == "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } else { print "<HTML>\n"; print "<HEAD><TITLE>Patton Industries</TITLE><BASEFONT SIZE=5></HEAD>\ +n"; print "<H1>Bonus Calculation</H1>\n"; print "<BODY>\n"; print "Salesperson: $Salesperson<BR>\n"; printf "Your bonus is: \$%.2f<BR><BR>\n", $bonus; printf "You entered a sales amount of \$%.2f and a \n", $Sales; printf "bonus rate of %.1f%%.<BR>\n", $Rate * 100; print "</BODY>\n"; print "</HTML>\n"; print "</BODY></HTML>\n"; }
      Let me explain in short words. An HTTP transaction consists of two parts. The first part is the headers, that consist of special data to tell the client how to interpret the data. The second part is the body. Headers are series of name-value pairs seperated by new lines. The header section as a whole is ended by an empty new line. So for example:
      print "Content-type: text/html\n"; print "Location: foo\n\n"; print "This is the body"
      You print two headers: Content-type, and location, then you send "this is the body" as the body of the message. However, if you do this:
      print "Content-type: text/html\n\n"; print "Location: foo\n"; print "This is the body"
      You are only sending one header, the Content-type. The Location: foo line and the "this is the body" is all interpreter as part of the body of the response.
      As a side note: you mention two times your book doesn't contain enough information to solve your problem. Maybe you should try to get better documentation. The reference manual for CGI.pm should be accessible via man CGI (at least on unixoid systems). And have a look at Book Reviews...
Re^2: HTTP headers and redirection
by student (Novice) on Nov 11, 2004 at 04:19 UTC
    Thanks for your valuable input. It is greatly appreciated. :)

      In reading your responses to the posts, I'm not sure if you solved your problem.

      What you need to do is remove this line (line 2 or 3)
      print "Content-type: text/html\n\n";

      and put it after the last else, just before
      print "<HTML>\n";

        I moved it and it still doesn't work...this is getting frustrating. If I leave my print location line at the top it gives me a page could not be displayed. When I leave it like it is below it gives me an internal server error. Any ideas?
        #!c:\phpdev\perl\bin\perl.exe use CGI qw(:standard -debug); #prevent Perl from creating undeclared variables use strict; #declare variables my ($Salesperson, $Sales, $Size, $Rate, $Percentage, @records, @errors +, $errors, $bonus); #assign input items to variables $Salesperson = param('Salesperson'); $Sales = param('Sales'); $Rate = param('Rate'); $Percentage = param('Percentage'); #calculate bonus rates $bonus = $Rate * $Sales; if ($Salesperson eq "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } elsif ($Sales == "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } elsif ($Rate == "") { print "Location: http://localhost/cgi-bin/c12ex3b.html\n\n"; } else { print "Content-type: text/html\n\n"; print "<HTML>\n"; print "<HEAD><TITLE>Patton Industries</TITLE><BASEFONT SIZE=5></HEAD>\ +n"; print "<H1>Bonus Calculation</H1>\n"; print "<BODY>\n"; print "Salesperson: $Salesperson<BR>\n"; printf "Your bonus is: \$%.2f<BR><BR>\n", $bonus; printf "You entered a sales amount of \$%.2f and a \n", $Sales; printf "bonus rate of %.1f%%.<BR>\n", $Rate * 100; print "</BODY>\n"; print "</HTML>\n"; print "</BODY></HTML>\n"; }

        Student,
        The last set of code you posted is correct and should not give a fatal error.

        Add this line after your 'use CGI'
        use CGI::Carp(qw/fatalsToBrowser/);

        That will print the error to your browser to easily identify the problem.