Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

CGI 'start_html' not printing opening <body> or <html>

by spickles (Scribe)
on Mar 04, 2009 at 22:59 UTC ( #748350=perlquestion: print w/ replies, xml ) Need Help??
spickles has asked for the wisdom of the Perl Monks concerning the following question:

I am using the CGI module to write an html page. I'm using OO, so $q = new CGI; and $q->end_html prints the closing </body> and </html> tags fine. However, $q->start_html isn't printing the opening tags? Code below for the page and its associated 'view source'. Thanks.

sub sectionOne() { $q->header(); style (); $q->start_html(); print "Enter the Number of Buildings:\n"; print "<form name=\"form1\" action=\"add_building_names.cgi\" method=\ +"POST\">\n"; print "<table style=\"border-collapse:separate;padding:0px;width:100px +;font-size:8pt\">\n"; print "<tr>\n"; print "<td><input type=\"text\" name=\"num_buildings\" size=\"5\"></td +>\n"; print "<td><input type=\"submit\" name=\"submit_num_buildings\" value= +\"Update\"></td>\n"; print "</tr>\n"; print "<input type=\"hidden\" name=\"section_number\" value=\"2\">\n"; print "</form>\n"; print "</table>"; $q->end_html(); }


Here is the 'view source'

<style type="text/css"> table { border:solid black 0px; padding:10px; border-collapse:separate; width:600px; } td { border:solid black 1px; padding:5px; font-weight: bolder; text-align: center; } </style> Enter the Number of Buildings: <form name="form1" action="add_building_names.cgi" method="POST"> <table style="border-collapse:separate;padding:0px;width:100px;font-si +ze:8pt"> <tr> <td><input type="text" name="num_buildings" size="5"></td> <td><input type="submit" name="submit_num_buildings" value="Update"></ +td> </tr> <input type="hidden" name="section_number" value="2"> </form> </table> </body> </html>

Comment on CGI 'start_html' not printing opening <body> or <html>
Select or Download Code
Re: CGI 'start_html' not printing opening <body> or <html>
by bellaire (Hermit) on Mar 04, 2009 at 23:10 UTC
    You need to print the return values of those calls.
    print $q->header(); print $q->start_html();
    The same should hold true for end_html, I'm not sure why view source shows that output for you, when I run your source from the command line, I don't see the closing body or html tags.
Re: CGI 'start_html' not printing opening <body> or <html>
by tilly (Archbishop) on Mar 04, 2009 at 23:10 UTC
    CGI just returns the text, it does not print it, so you have to print that for yourself. You also will need to print the header.

    If the end_html stuff is being printed, then I am guessing that whoever called the poorly named sectionOne function is printing what it returns, and end_html is what gets returned and therefore printed.

    As a random note, with most ways of organizing code, you are better off using a templating system and not using CGI's html generation methods. This is not a hard and fast rule, but it is a good guideline. See Re (tilly) 6: Code Critique for some general advice on templating.

Re: CGI 'start_html' not printing opening <body> or <html>
by cormanaz (Chaplain) on Mar 04, 2009 at 23:14 UTC
    You'll have a better chance of getting a good answer if you post all the code. From this it's not clear how you're passing the CGI object $q to the sub. It's not an argument. Is it defined as a global? Also what's this style sub you're calling?
      Below is the full code. I use the subroutines to print the HTML page based on the input required. The purpose of the page is to ask a user how many buildings are being worked on. I then reload the page, updating the section number to be the next section, and render the HTML pertinent to that section. The sub 'sectionTwo()' renders a table containing the number of input text boxes indicated in 'sectionOne()' by the user. The user enters the building names and proceeds to 'sectionThree()'. Section three shows a table that basically repeats the data input by the user and asks him/her to confirm that the data is correct prior to entering it into the database ('sectionFour()'). The sub 'style()' is simply a CSS sheet that gives me simple borders around my tables so I can see the layout. Once I get the forms working, I'll create a final CSS sheet and reference it in the 'start_html()'.
      Thanks.
      #!h:/xampp/perl/bin/perl -w # PERL MODULES WE WILL BE USING use DBI; use DBD::mysql; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use dbConnect; require "ap_messages_config.pl"; #PULL IN THE FILE THAT CREATES EACH H +TML RESPONSE PAGE #INCLUDES THE SUB 'STYLE' FOR CSS $q = new CGI; print $q->header(); my @values = split(/=/,$ENV{QUERY_STRING}); my $customer_get = $values[1]; my $customer_post = $q->param('customer_post'); my $num_buildings = ""; my $errors = ""; my @parameters = $q->param(); #THIS JUST STORES THE NAMES OF THE PARAM +ETERS. TO PRINT ITS VALUE, USE #param($parameters[0]) . "<br>\n"; ###################################################################### +########################################### ############################## BEGIN MAIN PROGRAM #################### +########################################### ###################################################################### +########################################### #style (); print $q->start_html(); if (($q->param('section_number')) < 2) { sectionOne($customer_get); } if (($q->param('section_number')) eq "2") { sectionTwo(); } if (($q->param('section_number')) eq "3") { sectionThree($customer_post); } if (($q->param('section_number')) eq "4") { sectionFour(); } print $q->end_html(); ###################################################################### +########################################### ############################## BEGIN MAIN PROGRAM #################### +########################################### ###################################################################### +########################################### ############################################## SUBROUTINES ########### +########################################### =pod sub javaScript() { # USE CGI 'start_html' TO ADD JAVASCRIPT TO HEADER print $q->start_html( -title=>'Enter AP Names', -script=> { -language => 'javascript', -src => '../scripts/hideDiv.js' } ); style (); } =cut ################################## HTML PAGE TO ASK USER NUMBER OF BUI +LDINGS ##################################### sub sectionOne() { print "Section One.<br>\n"; print "The value of \$customer_get is: $customer_get.<br>\n"; print "The value of \$customer_post is: $customer_post.<br>\n"; print "Enter the Number of Buildings:\n"; print "<form name=\"form1\" action=\"add_building_names.cgi\" method=\ +"POST\">\n"; print "<table style=\"border-collapse:separate;padding:0px;width:100px +;font-size:8pt\">\n"; print "<tr>\n"; print "<td><input type=\"text\" name=\"num_buildings\" size=\"5\"></td +>\n"; print "<td><input type=\"submit\" name=\"submit_num_buildings\" value= +\"Update\"></td>\n"; print "</tr>\n"; print "<input type=\"hidden\" name=\"section_number\" value=\"2\">\n"; print "<input type=\"hidden\" name=\"customer_post\" value=\"" . $_[0] + . "\">\n"; print "</form>\n"; print "</table>"; } #print param('num_buildings') . "<br>\n"; #THIS WORKS #print param($parameters[0]) . "<br>\n"; #THIS WORKS TOO. ################################### HTML PAGE TO RENDER INPUT BOXES FO +R BUILDING NAMES ############################ sub sectionTwo() { print "Section Two.<br>\n"; print "The value of \$customer_get is: $customer_get.<br>\n"; print "The value of \$customer_post is: $customer_post.<br>\n"; #print "The length of \@parameters is: $#parameters.\n"; print "<table>\n"; print "<form name=\"building_names\" method=\"POST\" action=\"add_buil +ding_names.cgi\">\n"; my $num_buildings = $q->param('num_buildings'); for ($i=1; $i<=$num_buildings; $i++) { print "<tr>\n"; print "<td>Building: " . $i . "</td>\n<td><input type=\"text\" name=\" +building_" . $i . "\"></td>\n"; print "</tr>\n"; } print "<tr>\n"; print "<td>"; print "<input type=\"submit\" name=\"submit_building_names\" value=\"U +pdate\">\n"; print "</td>\n"; print "</tr>\n"; print "<input type=\"hidden\" name=\"section_number\" value=\"3\">\n"; print "</form>\n"; print "</table>\n"; } ###################################### PRINT AND CONFIRM BUILDING NAME +S ########################################3 sub sectionThree() { print "$_[0]<br>\n"; print "Section Three.<br>\n"; print "The value of \$customer_get is: $customer_get.<br>\n"; print "The value of \$customer_post is: $customer_post.<br>\n"; my $customer = $_[0]; print "The value of \$customer (passed in to sub) is: $customer.<br>\n +"; #Store the building names to variables for ($i = 0; $i<$#parameters; $i++) { #print $q->param($parameters[$i]) . "<br>\n"; if (($parameters[$i]) =~ /^building/) { my @buildings = push (@buildings,($q->param($parameters[$i]))); } } $j = scalar (@buildings); my @building_nums; for ($k = 1; $k<=$j; $k++) { push (@building_nums,$k); } print "<form name=\"submit_bldg_names\" method=\"POST\" action=\"add_b +uilding_names.cgi\">\n"; print "<table>\n"; print "<tr>\n"; print "<td>You entered the following building names. If they are corr +ect, select 'Submit' to add them to the database for customer $customer</td>\n"; print "</tr>\n"; print "<tr>\n"; print "<td>"; print "<input type=\"submit\" name=\"add_bldgs_database\" value=\"Subm +it\">"; print "</td>\n"; print "</tr>\n"; for ($i = 0; $i<=$#buildings; $i++) { print "<tr>\n"; print "<td>"; print "Building $building_nums[$i]: $buildings[$i]"; print "<input type=\"hidden\" name=\"building_" . $i . "\" value=\"" . + $buildings[$i] . "\">"; print "</td>\n"; print "</tr>\n"; } print "<input type=\"hidden\" name=\"section_number\" value=\"4\">\n"; print "</table>\n"; print "</form>"; } ######################## ADD BUILDING NAMES TO THE DATABASE TABLE #### +#################################### sub sectionFour() { } ###################################################################### +####################################
        Fixed it. Sloppy code in my 'style()' sub. I was making another call to $q-> new CGI; when I didn't need it. I removed the external subroutine all together and placed 'sub style()' in the main program. Works fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://748350]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (17)
As of 2015-07-02 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls