in reply to CGI 'start_html' not printing opening <body> or <html>

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?
  • Comment on Re: CGI 'start_html' not printing opening <body> or <html>

Replies are listed 'Best First'.
Re^2: CGI 'start_html' not printing opening <body> or <html>
by spickles (Scribe) on Mar 05, 2009 at 05:20 UTC
    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.