Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

CGI && tables nested within tables.

by Macphisto (Hermit)
on Dec 28, 2000 at 23:21 UTC ( [id://48668]=perlquestion: print w/replies, xml ) Need Help??

Macphisto has asked for the wisdom of the Perl Monks concerning the following question:

Monkeys,
I'm trying to write a CGI that generates a dynamic table within another table also generated via CGI. I wrote a small test script to display what I want and it is posted below.
#!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; print $q->header( -type => "text/html"), $q->start_html, $q->start_table(), $q->Tr( $q->td("Cell1"), $q->td("Cell2") ), $q->Tr( $q->td("Cell3"), $q->td(nested()) ), $q->end_table, $q->end_html; sub nested { $q->start_table(), $q->Tr( $q->td("Cell4"), $q->td("Cell5") ), $q->Tr( $q->td("Cell6"), $q->td("Cell7") ), $q->end_table, }

This produces:
Cell1 Cell2
Cell3
Cell4 Cell5
Cell6 Cell7

When I attempt to change the code so the second table is created by a simple for loop like to the following:
#!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; print $q->header( -type => "text/html"), $q->start_html, $q->start_table(), $q->Tr( $q->td("Cell1"), $q->td("Cell2") ), $q->Tr( $q->td("Cell3"), $q->td(nested()) ), $q->end_table, $q->end_html; sub nested { $q->start_table(); for (my $x = 4; $x < 8; $x++) { print $q->Tr( $q->td("cell ". $x), $q->td("cell ". $x+1) ); $x++; } print $q->end_table; }

I get:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML><HEAD><TITLE>Untitled Document</TITLE> </HEAD><BODY><TABLE><TR><TD>Cell1</TD> <TD>Cell2</TD></TR><TR><TD>Cell +3</TD> <TD>1</TD></TR></TABLE></BODY></HTML>

printed to the browser window. I'm curious as to what would cause this. I have a feeling I'm just overlooking something but as of yet I have not found said "thing."

As a side note: The code I am working on is Here but isn't having the same problem...instead the second table is printed first, and then the first table is printed beneath the second. If you're up for charity work you could take a look at that one as well. The code for it is posted Here
System Notes Just to make sure all needed info is given this is using 5.005_03 on Linux Redhat 6.2 and tested under Apache. If more info is needed I'll post it here if it is asked for

Regards,
Macphisto


Everyone has their demons....

Replies are listed 'Best First'.
Re: CGI && tables nested within tables.
by chromatic (Archbishop) on Dec 28, 2000 at 23:41 UTC
    Besides what chipmunk said (about ten seconds before I did), I got a couple of non-numeric errors about the concatenation where you add one to $x. here's my rewrite of nested():
    sub nested { my $str = $q->start_table( -border => 1); for (my $x = 4; $x < 8; $x++) { $str .= $q->Tr( $q->td("cell ". $x), $q->td("cell ". $x++) ); } return $str . $q->end_table; }
Re: CGI && tables nested within tables.
by chipmunk (Parson) on Dec 28, 2000 at 23:38 UTC
    Your old nested() function returned some HTML, but your new nested() function is printing its HTML before the main code has printed the header or anything else. The text printed by nested() is breaking your HTTP headers, which produces the result you're reporting.

    Solution: nested() should return the HTML instead of printing it.

    Note that running your CGI script from the command line probably would have revealed the problem.

      Thanks Chipmunk,
      I was struggling with that and just didn't look at it as one giant print statement thus didn't realize it wouldn't have finished until the ';' I'm very impressed that you caught that so quickly. You answered in mere minutes.
      p.s. You mentioned running the cgi at the command line. Are you referring to 'perl -wcT foo.cgi' or something else?

      Everyone has their demons....
        Yes, you can run your script from the command line to see what output it produces, including the HTTP headers which the browser doesn't show you. Something like: `perl -wT foo.cgi`. Use -c to make sure the script compiles, then leave it off to see what output you get.

        Because CGI scripts get their input from the environment and standard input, you can run your script from the command line with all the data it would get from the web server. For example, you can set QUERY_STRING in your shell's environment to 'id=7&name=bob'.

        The CGI module provides a debug mode when run under the shell to make this easier. Run the script from the shell, and you can include parameters and values on the command line or pass them through standard input. (See the DEBUGGING section of the manpage for more details.)

Re: CGI && tables nested within tables.
by epoptai (Curate) on Dec 29, 2000 at 05:51 UTC
    Just an observation:

    If you only use one CGI query object you can use the default CGI object by invoking something like:

    use CGI qw(:standard);
    Then you don't have to use the $q-> object:
    print $q->header; becomes print header;

    If you know what methods you'll be using you can import them individually:

    use CGI qw(param header url); my $url = url();

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-28 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found