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

CGI output to existing HTML file 1

Status
Not open for further replies.

CGIflustered

Technical User
Feb 12, 2002
11
US
Hello,

I am new to CGI. I've been able to configure a guestbook script to work fine... Now I am trying to modify it (or another script) to take form data and output the results to a specific HTML file. I'm not sure how to do this.

It's likely a simple solution (but I'm a simple guy). I need to create a changing HTML form that is renewed each time a new form is submitted. The guestbook accomplishes this, but keeps a running log... I need the old HTML form to be completely overwritten with the new HTML.

So far, I've only been able to make a HTML page report back to the browser, but I have no idea how to make it save to the actual HTML file.

Thanks for your suggestions.
Mark
 
# Normal cgi stuff previous to this.....
# then, when ready to write your new content to disk.....

# open a file to print to....this will OVERWRITE an existing file.
open(OPF,">some_file_name.html") or ooops("Failed to open some_file_name.html $!");

# print your file content to that file handle, "OPF"
print OPF "
<html>
<head>
<title>building a page on disk</title>
</head>
<body>
<p>The stuff you want put it the file</p>
</body>
</html>&quot;;
close OPF;
# your html is written to disk in file - some_file_name.html


sub ooops
{
my $ooops = shift;
print qq(<p>$ooops</p></body></html>);
exit;
} 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks, go Boating... It started me in the right direction, but I'm encountering &quot;Internal server error&quot; or &quot;possible misconfiguration&quot; errors when the cgi is called. :(

Here's what I'm working with so far, modified from another script. Anyone spot any errors?:

# Define Variables
$mailprog = '/usr/lib/sendmail';
$date = &quot;/usr/bin/date&quot;; chop($date);
$prodpassdown = &quot;/users/martuso/public_html/Passdown/1st/prod1.html&quot;;

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;

$FORM{$name} = $value;
}

# Print Return HTML
open(OPF1,&quot;$prodpassdown&quot;) || ooops &quot;Failed to open $prodpassdown: $!\n&quot;;

print OPF1 &quot;Content-type: text/html\n\n&quot;;
print OPF1 &quot;<html><head><title>Production Passdown - 1st Shift</title></head>\n&quot;;
print OPF1 &quot;<CENTER>\n&quot;;
print OPF1 &quot;<font face=\&quot;arial,helvetica\&quot;>\n&quot;;
print OPF1 &quot;<body bgcolor=\&quot;eoffff\&quot; text=\&quot;black\&quot; link=\&quot;cyan\&quot; vlink=\&quot;cyan\&quot;>\n&quot;;
print OPF1 &quot;<h2>Production Passdown - 1st Shift</h2>\n&quot;;
print OPF1 &quot;<h3>$FORM{d1}</h3>\n&quot;;
print OPF1 &quot;<br><br>\n&quot;;
print OPF1 &quot;<b>Entered by: </b> $FORM{n1}<BR>\n&quot;;
print OPF1 &quot;<BR>\n&quot;;
print OPF1 &quot;<BR><hr>\n&quot;;
print OPF1 &quot;This passdown was generated on $date. Form Revised 02/14/02<BR>\n&quot;;
print OPF1 &quot;</font></body></html>\n&quot;;
close(OPF1);

sub ooops
{my $ooops = shift;
print qq(<p>$ooops</p></body></html>);
exit;
}


Thanks in advance,
Mark
 
Whoops! I found the missing &quot;>&quot; when I try to open the file, but it still doesn't work. Here's the same, non-working program trimmed down a tiny bit:

# Define Variables

$date = &quot;/usr/bin/date&quot;; chop($date);
$prodpassdown = &quot;/users/martuso/public_html/Passdown/1st/prod1.html&quot;;

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;

$FORM{$name} = $value;
}

# Print Return HTML
open(OPF1,&quot;>$prodpassdown&quot;);

print OPF1 &quot;Content-type: text/html\n\n&quot;;
print OPF1 &quot;<html><head><title>Production Passdown - 1st Shift</title></head>\n&quot;;
print OPF1 &quot;<CENTER>\n&quot;;
print OPF1 &quot;<font face=\&quot;arial,helvetica\&quot;>\n&quot;;
print OPF1 &quot;<body bgcolor=\&quot;eoffff\&quot; text=\&quot;black\&quot; link=\&quot;cyan\&quot; vlink=\&quot;cyan\&quot;>\n&quot;;
print OPF1 &quot;<h2>Production Passdown - 1st Shift</h2>\n&quot;;
print OPF1 &quot;<h3>$FORM{'d1'}</h3>\n&quot;;
print OPF1 &quot;<br><br>\n&quot;;
print OPF1 &quot;<b>Entered by: </b> $FORM{'n1'}<BR>\n&quot;;
print OPF1 &quot;</font></body></html>\n&quot;;
close(OPF1);

Grrrr... CGI hates me. Any ideas anybody?

Thanks,
Mark
 
A few things....

[red]
Code:
# send some compliant html back to the browser
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head></head><body><p>junk</p>&quot;;


# it appears that your are trying to get the date from the OS
# if you want to run an OS command, use backticks, not single
# quotes, but backticks - below the tilde ~.  Like,
# $date = `/usr/bin/date`; chop ($dates); are good in bread
[/red]LOL , sorry
Code:
$date = &quot;/usr/bin/date&quot;; chop($date);
[red]
Code:
# also ,add some error checking to that &quot;$date =...&quot; stuff, 
# maybe like,
unless ($date =~ /\w+/) { &ooops(&quot;Failed to get the date&quot;); }

# make sure that the httpd daemon has sufficient permissions to write
# this file.
[/red]
Code:
$prodpassdown = &quot;/users/martuso/public_html/Passdown/1st/prod1.html&quot;;

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 
# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs){
   ($name, $value) = split(/=/, $pair);

   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
   $name =~ tr/+/ /;
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
          
   $FORM{$name} = $value;
}
# if this file will be servered directly by the web server, 
# then you don't need the content-type in it.  The web server
# adds that on the fly to static html pages.
# Print Return HTML
open(OPF1,&quot;>$prodpassdown&quot;)
[red]
Code:
or ooops(&quot;Failed to open OPF1&quot;);
[/red]
Code:
   print OPF1 &quot;Content-type: text/html\n\n&quot;; # probably don't need this
   print OPF1 &quot;<html><head><title>Production Passdown - 1st Shift</title></head>\n&quot;;
   print OPF1 &quot;<CENTER>\n&quot;;
   print OPF1 &quot;<font face=\&quot;arial,helvetica\&quot;>\n&quot;;
   print OPF1 &quot;<body bgcolor=\&quot;eoffff\&quot; text=\&quot;black\&quot; link=\&quot;cyan\&quot; vlink=\&quot;cyan\&quot;>\n&quot;;
   print OPF1 &quot;<h2>Production Passdown - 1st Shift</h2>\n&quot;;
   print OPF1 &quot;<h3>$FORM{'d1'}</h3>\n&quot;;
   print OPF1 &quot;<br><br>\n&quot;;
   print OPF1 &quot;<b>Entered by: </b> $FORM{'n1'}<BR>\n&quot;;
   print OPF1 &quot;</font></body></html>\n&quot;;
close(OPF1);
print &quot;</body></html>&quot;;
[red]
Code:
# since 'die' won't show up in the browser, use a simple
# sub to report errors.  If you use the CGI.pm module, 
# some of this gets a little easier.
#-----------------------------------------------------
sub ooops
{
# something did not work - print the error and exit
my $error = shift; # get first element from @_
print &quot;<p>$error</p></body></html>&quot;;
exit;
}
[/red]

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
<!--&quot;ACK!&quot;-->

I've tried the suggestions (thanks for bearing with me!)... Still says:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
--------------------------------------------------------------------------------
Apache/1.3.14 Server at Port 80

I know I have the permissions set correctly (I have a guestbook script that works just fine). Here's what the entire nastiness looks like now:

#!/usr/local/bin/perl
#%Z%%M% %I% %G% %U% VSC

####################################################
# FormMail
# Shamelessly modified by Mark Artuso
####################################################
# Define Variables

$date = `/usr/bin/date`; chop($date);
$prodpassdown = &quot;/users/martuso/public_html/Passdown/1st/prod1.html&quot;;

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;

$FORM{$name} = $value;
}

# send some compliant html back to the browser
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head></head><body><p>For God's sake, please work</p>&quot;;

# Print Return HTML
open(OPF1,&quot;>$prodpassdown&quot;) or ooops(&quot;Failed to open OPF1&quot;);

print OPF1 &quot;<html><head><title>Production Passdown - 1st Shift</title></head>\n&quot;;
print OPF1 &quot;<body bgcolor=\&quot;eoffff\&quot; text=\&quot;black\&quot; link=\&quot;cyan\&quot; vlink=\&quot;cyan\&quot;>\n&quot;;
print OPF1 &quot;<CENTER>\n&quot;;
print OPF1 &quot;<font face=\&quot;arial,helvetica\&quot;>\n&quot;;
print OPF1 &quot;<body bgcolor=\&quot;eoffff\&quot; text=\&quot;black\&quot; link=\&quot;cyan\&quot; vlink=\&quot;cyan\&quot;>\n&quot;;
print OPF1 &quot;<h2>Production Passdown - 1st Shift</h2>\n&quot;;
print OPF1 &quot;<h3>$FORM{'d1'}</h3>\n&quot;;
print OPF1 &quot;<br><br>\n&quot;;
print OPF1 &quot;<b>Entered by: </b> $FORM{'n1'}<BR>\n&quot;;
print OPF1 &quot;</CENTER></font></body></html>\n&quot;;
close(OPF1);

sub ooops
{
my $error = shift;
print &quot;<p>$error</p></body></html>&quot;;
exit;
}

Help me Obi Wan goBoating, your my only hope!

Mark
 
I copied your code into a text editor, tweaked a couple of paths,
- saved it to disk in my cgi-bin and
- converted from dos2unix line endings
- chmod +x your_code.cgi

Opened a browser, hit the uri and got,
&quot;For God's sake, please work&quot;

So, your perl works. You must be mis-handling something relative
to the server.

Did you remember to:
? convert from dos line endings to unix line endings?
? make sure the execute bit was set?
? does the path to the file you want to write (prod1.html) exist?
? does the web server have sufficient permissions to open/create the
file in that directory?

If I were you, I'd back up a step or two and try something brutally simple.
Try this code.
Code:
#!/usr/local/bin/perl
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head><title>very simple CGI</title></head>
       <body><p>Some very simple stuff</p></body></html>&quot;;
[code]

Save that into a file in a valid cgi-bin directory.
Convert line endings, if necessary.  It appears that you are 
   running on a *nix box.  If you are doing your editting on
   a PC, then you will need to convert you line endings.
Set the execute bit on the cgi file, >chmod +x your_code.cgi
Go to a browser and hit that URI.  You should see the message.
If not, then let's pick up there and figure out why not.

If that did work, then lets expand it just a little to open 
a text file and write to it.
[code]
#!/usr/local/bin/perl
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head><title>very simple CGI</title></head>
       <body><p>Some very simple stuff</p>\n&quot;;

open(OPF,&quot;>some_file.txt&quot;) or 
     barf(&quot;<p>Failed to open file</p></body></html>&quot;);
print OPF &quot;Some text to go into the file\n&quot;;
close OPF;
print&quot;<p>must have worked if this text printed</p></body></html>&quot;;

sub barf {   print &quot;@_&quot;;   exit; }

That should successfully open the new file and print the line into it.
If the previous piece of code worked and this one does not, then
let us know what error you are getting and we'll go from there.




'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Oh my lord... I went to upload your sample program, and saw I was doing all my transfers in binary mode... When I transferred to ASCII mode it worked!

&quot;Ahem&quot;... my other script is working now. Thank you for all your help goBoating, it's good to know there are helpful folks like yourself out here!

Mark

PS: Trust me, I'll most likely bother you with some other newbie problem soon ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top