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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI and forms problem 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi,

I have written the following script cgi script called asi.cgi. This scripts reads data from a file and create a simple table. What I would like to add is the possibility to export this table in a file that will be interpreted in Excel. I would like the same script to do both so can I click on an icon (wow_disquette.gif) the cgi will know that the user wants to export to Excel.

My question is can a CGi call itself? Also as I am new to FORMS I am not sure if I did the things correctly. Nothing prints with the command print "variables: $nam, $val\n";

Thanks!

#!/usr/bin/perl


$data_file="asidata.cgi";

open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);


$buf = $ENV{'QUERY_STRING'};

@pairs=split(/&/,$buf);
foreach $pair (@pairs) {
($nam,$val) = split(/=/,$pair);
print "variables: $nam, $val\n";
}

print << "HTML";
Content-type: text/html; charset=iso-8859-1

<html>
<head>
<title>OpenWorks Projects General</title>
<script src="/wow/wow_css.js"></script>
<script src="/wow/wow_popupwin.js"></script>
</head>
<body>
<FORM action=" method="get">
<INPUT type="hidden" name="save" value="yes">
<H2>OpenWorks Projects General
<INPUT type=image align=absbottom border=0 src="/wow/images/wow_diskette.gif"></H2>
</FORM>

<table border cellspacing=0.5 cellpadding=1>
<tr>
<th>Project&nbsp;</th>
<th>Last Modified&nbsp;</th>
<th>Last User&nbsp;</th>
<th>Tablespace&nbsp;</th>
<th>External files&nbsp;</th>
<th>Nb SW prj&nbsp;</th>
<th>SW size&nbsp;</th>
<th>Total size&nbsp;</th>

</tr>
<tr>

HTML

foreach $line (@raw_data)
{
chop($line);
($w_name,$last_mod,$last_user,$tblsp,$extf,$nbSW,$SWs,$tot)=split(/ /,$line);

#print"<td><a href=\"# owproj=$w_name\">$w_name</a></td>\n";

if ($w_name ne "PROJECT" ) {
print" <td>$w_name</td>\n";
print" <td>$last_mod</td>\n";
print" <td>$last_user</td>\n";
print" <td>$tblsp</td>\n";
print" <td>$extf</td>\n";
print" <td>$nbSW</td>\n";
print" <td>$SWs</td>\n";
print" <td>$tot</td>\n";
print" </tr>\n";
print" <tr> \n";
}
}
print << "HTML2";
</table>
</body>
</html>

HTML2
 
Sure, make your excel link point back to the cgi with a new argument that indicates excel as the output format.

If excel is selected then generate the proper excel format and some new headers to indicate its an excel file and push it out.

There are some threads in the perl forum on the topic of outputting Excel from perl and having it directly download to the hardrive with a proper filename.

In regards to your $var/$name question, why are you parsing the CGI args yourself? Use CGI.pm, its built for this and comes with Perl.. Trust me, it will do a better job then you parsing all the varieties of CGI args you will get.



 
Thanks, I had a quick look at CGI.pm and it seems quite complicated to me..

My problem is not to write the Excel form which as you said is merely writing a new header. My problem is to have just one cgi script generating both and html page with a table and generating the excel file when clicking on an image.

I could not see print variables: $nam, $val\n because I did not include them in the HTML part! Now that it is done when I click on the link I get

Variables: y 7
If I click again
Variables: y 8

Where do those come from?


 
Ok, take this :

Code:
print << "HTML";
Content-type: text/html; charset=iso-8859-1

<html>
<head>
 <title>OpenWorks Projects General</title>
 <script src="/wow/wow_css.js"></script>
 <script src="/wow/wow_popupwin.js"></script>
</head>
<body>
<FORM action="[URL unfurl="true"]http://pemba.hq.ep.corp.local:8989/bin/asi.cgi"[/URL] method="get">
<INPUT type="hidden" name="save" value="yes">
<H2>OpenWorks Projects General
        <INPUT type=image align=absbottom border=0 src="/wow/images/wow_diskette.gif"></H2>
</FORM>

<table border cellspacing=0.5 cellpadding=1>
<tr>
  <th>Project&nbsp;</th>
  <th>Last Modified&nbsp;</th>
  <th>Last User&nbsp;</th>
  <th>Tablespace&nbsp;</th>
  <th>External files&nbsp;</th>
  <th>Nb SW prj&nbsp;</th>
  <th>SW size&nbsp;</th>
  <th>Total size&nbsp;</th>

</tr>
<tr>

HTML

foreach $line (@raw_data)
{
 chop($line);
 ($w_name,$last_mod,$last_user,$tblsp,$extf,$nbSW,$SWs,$tot)=split(/ /,$line);

#print"<td><a href=\"[URL unfurl="true"]http://pemba.hq.ep.corp.local:8989/bin/ow_proj_summary.cgi?[/URL]
#    owproj=$w_name\">$w_name</a></td>\n";

if ($w_name ne "PROJECT" ) {
    print"  <td>$w_name</td>\n";
    print"  <td>$last_mod</td>\n";
    print"  <td>$last_user</td>\n";                
    print"  <td>$tblsp</td>\n";
    print"  <td>$extf</td>\n";
    print"  <td>$nbSW</td>\n";
    print"  <td>$SWs</td>\n";
    print"  <td>$tot</td>\n";
    print" </tr>\n";
    print" <tr> \n"; 
    }
}
print << "HTML2";
</table>
</body>
</html>

HTML2


and stick it in a function

Then take your excel export code and stick IT in a function.

Then have a link with a new argument, if you detect that argument export excel, otherwise export html.

I'd do it like this :

Code:
#!/usr/bin/perl


$data_file="asidata.cgi";

open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

use CGI ;
if($query->param('EXPORTEXCEL') ) {
  doExcelExport();
} else {
  doHTMLExport();
}

then doExcelExport contains your excel export code and doHTMLExport has your html code that you already have above.

Make your excel link look like

yourcgi.cgi?EXPORTEXCEL=1

and your set.

This is not a verbatim example, Some Assembly Required.
 
OK so I don't need to write the form
<FORM action=" method="get">
<INPUT type="hidden" name="save" value="yes">
<H2>OpenWorks Projects General
<INPUT type=image align=absbottom border=0 src="/wow/images/wow_diskette.gif"></H2>
</FORM>

then?
 
Siberian,

Your example does not work. I created the two functions +added your priced of code and now I get internal server error with perl -wc I get
Name "main::query" used only once: possible typo at asi.cgi line 11.
asi.cgi syntax OK

that's because query is not initialized?
 
Like I said

This is not a verbatim example, Some Assembly Required.
I am not giving you exact and specific code, I am giving you a path to explore.

This is not a difficult problem, you just need to conceptually understand it.

If the user clicked excel output, do something to output excel, if they did not click excel output send the html.

 
Either of those worked:

$query = new CGI;

if($query->param('EXPORTEXCEL') ) {
&doExcelExport();
} else {
&doHTMLExport();
}

or

if (param()) {
&doExcelExport();
} else {
&doHTMLExport();
}

Many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top