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!

How do I get a script to show an html page in the browser?

Status
Not open for further replies.

smashing

Programmer
Oct 15, 2002
170
US
Am very new to CGI & Perl, have succesfully designed a form, written a script to parse it and email it to my client. Meanwhile, people submitting the form get a simple "thank you" (print "thank you";). I have a nice 'thankyou.html 'page that I designed that is part of the web site (includes a lot of stuff like headers, links, floating boxes, e.t.c) is there a way to get the script to print that page in the user's browser upon submitting the form? In Perl there's gotta be a way!
 
You can use a redirect...
Code:
#!/usr/local/bin/perl
use CGI;
my $query = new CGI;

# Do your database stuff
# If all database stuff was successful, $ok = 1;
# else, $ok = '';

if ($ok) {
    print $query->redirect('[URL unfurl="true"]http://somewhere.else/thankyou.html');[/URL]
    }
else {
    print $query->redirect('[URL unfurl="true"]http://somewhere.else/db_error.html');[/URL]
    }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Dear Goboating
Thanks for the reply.
First don't forget I'm very new to this stuff, and a little [bugeyed])
I tried but not getting result I want.

You wrote
# Do your database stuff
# If all database stuff was successful, $ok = 1;
# else, $ok = '';

Maybe you could explain that a little further.

Anyway, there's no database involved at all in my script (see copy below) so I just subsituted last line of script with

print redirect ('
the result I get: Status: 302 Moved Location: Same result if I declared
my $query = new CGI;
at the top and
print $query->redirect('at the end. Please help!!! Thanx

(my existing script)
#! /usr/local/bin/perl
use strict;
use CGI ':standard';
my $name; my $contact; my $leavingfrom; my $destination; my $timeofday; my $email;
my $month; my $day; my @year; my $numberofpeople; my $emailaddress; my $comments; my $query;
$name = param('name');
$contact = param('contact');
$leavingfrom = param('leavingfrom');
$destination = param('destination');
$timeofday = param('timeofday');
$month = param('month');
$day = param('day');
@year = param('year');
$numberofpeople = param('numberofpeople');
$emailaddress = param('emailaddress');
$comments = param('comments');
$email = param();
print "Content-type: text/html\n\n";
open (MAIL, "|/usr/sbin/sendmail -t")
|| Eror ('open', 'mail program');
print MAIL "To: panamreserve\@hotmail.com\nFrom: $name\nSubject: Online Reservation for $month, $day, @year\n
<html><head><title>RESERVATION</title></head><body>
<h2>Reservation Information</h2>\n<b>Name: </b>$name\n
<p><b>Contact Telephone Number: </b>$contact\n
<p><b>Email Address: </b>$emailaddress\n
<p><b>Number of people in group: </b>$numberofpeople
<p><b>Leaving from: </b>$leavingfrom
<p><b>Destination: </b>$destination
<p><b>Date of travel: </b>$month, $day, @year, in the $timeofday
<p><b>Additional Comments:\n<p></b>$comments
</body></html>&quot;;
close (MAIL);
print &quot;Thank You Very Much&quot;;
 
You have a space in your #! line after the !.
This code has not been run. It may have (probably does have)
a typo or two, but, hopefully you can see the intended
flow through the code. If you are going to use
a redirect header, you don't print anything except that
header back to the browser (to STDOUT).

Code:
#!/usr/local/bin/perl
use strict;
use CGI;
my $query = new CGI:

# retrieve the information submitted on the form
# ' No reason to pre-declare your vars, just put
# the 'my' in when you create them.
my $name             = $query->param('name');
my $contact         = $query->param('contact');
my $leavingfrom     = $query->param('leavingfrom');
my $destination     = $query->param('destination');
my $timeofday         = $query->param('timeofday');
my $month             = $query->param('month');
my $day             = $query->param('day');
my @year             = $query->param('year');
my $numberofpeople    = $query->param('numberofpeople');
my $emailaddress     = $query->param('emailaddress');
my $comments         = $query->param('comments');
my $email             = $query->param('email');


# construct some test that indicates a reasonable form
# submission... something like make sure you got
# a name and reasonably well-formed email address
# if you did then send the email and redirect to the 
# thankyou page.  If you did not get a name and email, 
# Print a complaint with some instructions.
if (($name =~ /\w/) && ($email =~ /.*?@.*?\..*/))
    {
    &send_the_email; # send email
    &redirect(thanks); # call redirect sub for thanks
    }
else {
    &redirect(ooops); # Gripe about poorly filled form.
    }
exit;

# ------------
#          SUBS
#-----------------------------------------------
sub redirect
    {
    my $mode = shift;
    if ($mode eq 'thanks') # name and email where OK
        {   
   $query->redirect('[URL unfurl="true"]http://www.panambus.com/thankyou.html');[/URL] 
        }
    elsif ($mode eq 'ooops') # name or email weren't OK
        {
        print $query->header, 
            $query->start_html,
            qq(<p>Incomplete submission.  Please go back and 
                complete all data fields.</p>),
            $query->end_html;
        }
    }
#-----------------------------------------------
sub send_the_email
    {
    open (MAIL, &quot;|/usr/sbin/sendmail -t&quot;)
    || Eror ('open', 'mail program');
    print MAIL &quot;To: panamreserve\@hotmail.com\nFrom: $name\nSubject: Online Reservation for $month, $day, @year\n
    <html><head><title>RESERVATION</title></head><body>
    <h2>Reservation Information</h2>\n<b>Name: </b>$name\n
    <p><b>Contact Telephone Number: </b>$contact\n
    <p><b>Email Address: </b>$emailaddress\n
    <p><b>Number of people in group: </b>$numberofpeople
    <p><b>Leaving from: </b>$leavingfrom
    <p><b>Destination: </b>$destination
    <p><b>Date of travel: </b>$month, $day, @year, in the $timeofday
    <p><b>Additional Comments:\n<p></b>$comments
    </body></html>&quot;;
    close (MAIL);
    }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Dear Goboating
Thanx again!
Went ahead and copied your script, then checked it in Macperl for sytax errors results are

# syntax error, near &quot;new CGI:&quot;
File 'Untitled #2'; Line 4
# Bareword &quot;thanks&quot; not allowed while &quot;strict subs&quot; in use.
File 'Untitled #2'; Line 29
# Bareword &quot;ooops&quot; not allowed while &quot;strict subs&quot; in use.
File 'Untitled #2'; Line 32
# Untitled #2 had compilation errors.

So went ahead and changed
the : on line 4 to a ;
thanks to 'thanks' & ooops to 'ooops'
syntax checked again = # Untitled #2 syntax OK

Uploaded script but doesn't work now. result=

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@panambus.com 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.


Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Hope you can help!
also if you know where I can learn CGI some place easier than Elizabeth Castro's book &quot;Perl & CGI for the let me know.
Thanx again
 
oooops [blush]
I forgot the 'print' in front of the redirect statement.
That was a rather large typo. Sorry.

It should look like...
Code:
if ($mode eq 'thanks') # name and email where OK
  {
[red]print[/red]
Code:
 $query->redirect('[URL unfurl="true"]http://www.panambus.com/thankyou.html');[/URL]
        }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Hi again!
I really need to thank you for your time.
Looks like there is another typo somwhere in there 'cause added what you told me to & still getting same error message, although when testing in MacPerl result is syntax ok.
Just to make sure, I am pasting the new script here exactly as it is now.

#!/usr/local/bin/perl
use strict;
use CGI;
my $query = new CGI;
# retrieve the information submitted on the form
# ' No reason to pre-declare your vars, just put
# the 'my' in when you create them.
my $name             = $query->param('name');
my $contact         = $query->param('contact');
my $leavingfrom     = $query->param('leavingfrom');
my $destination     = $query->param('destination');
my $timeofday         = $query->param('timeofday');
my $month             = $query->param('month');
my $day             = $query->param('day');
my @year             = $query->param('year');
my $numberofpeople    = $query->param('numberofpeople');
my $emailaddress     = $query->param('emailaddress');
my $comments         = $query->param('comments');
my $email             = $query->param('email');
# construct some test that indicates a reasonable form
# submission... something like make sure you got
# a name and reasonably well-formed email address
# if you did then send the email and redirect to the
# thankyou page.  If you did not get a name and email,
# Print a complaint with some instructions.
if (($name =~ /\w/) && ($email =~ /.*?@.*?\..*/))
    {
    &send_the_email; # send email
    &redirect('thanks'); # call redirect sub for thanks
    }
else {
    &redirect('ooops'); # Gripe about poorly filled form.
    }
exit;
# ------------
#          SUBS
#-----------------------------------------------
sub redirect
    {
    my $mode = shift;
    if ($mode eq 'thanks') # name and email where OK
        {   
   print $query->redirect ('        }
    elsif ($mode eq 'ooops') # name or email weren't OK
        {
        print $query->header,
            $query->start_html,
            qq(<p>Incomplete submission.  Please go back and
                complete all data fields.</p>),
            $query->end_html;
        }
    }
#-----------------------------------------------
sub send_the_email
    {
    open (MAIL, &quot;|/usr/sbin/sendmail -t&quot;)
    || Eror ('open', 'mail program');
    print MAIL &quot;To: panamreserve\@hotmail.com\nFrom: $name\nSubject: Online Reservation for $month, $day, @year\n
    <html><head><title>RESERVATION</title></head><body>
    <h2>Reservation Information</h2>\n<b>Name: </b>$name\n
    <p><b>Contact Telephone Number: </b>$contact\n
    <p><b>Email Address: </b>$emailaddress\n
    <p><b>Number of people in group: </b>$numberofpeople
    <p><b>Leaving from: </b>$leavingfrom
    <p><b>Destination: </b>$destination
    <p><b>Date of travel: </b>$month, $day, @year, in the $timeofday
    <p><b>Additional Comments:\n<p></b>$comments
    </body></html>&quot;;
    close (MAIL);
    }


PLEASE NOTE though, that I have not made any thanks or oops messages, if I do need to do that please clarify.
Wating to here from you again. SO LONG!
 
Code:
my @year             = $query->param('year');
should be
Code:
my $year             = $query->param('year');
//Daniel
 
Nope &quot;danielhozac&quot;
Tried out your suggestion, but didn't work.
Anyway, reason for the @ versus the $ in that spot is because I have a 2 radio buttons (2002 & 2003) on the form to choose from and both have the same NAME of 'year' , but each has a differnt value respectively. To quote from the book (Elizabeth Castro's Perl & CGI For The edition p.105) &quot; If more than one of your HTML form elements has the the same NAME atttribute, it could have muliple values. You will thus have to assign the results of the PARAM function to an array instead of a scalar. And this worked fine for me in the first example of the script (in the second reply) above.
[cry]Still need help!!! Thanx
 
If I remember this part of the CGI specifications, you have to return a full set of headers even when you are redirecting. So you have to print $query->header and $query->redirect('url'), not just the latter. //Daniel
 
I copied your most recent version of the code.
I modified the first line to #!perl to work on the M$
machine I'm sitting in front of and replaced the
email sub-routine because I don't have sendmail on
this M$ machine. I put the file in my apache cgi-bin
and hit it with a browser.

With no arguments,
Code:
[URL unfurl="true"]http://localhost/cgi-bin/junk.pl[/URL]
I get the 'Incomplete submission' message as I should.
With a name and email like this,
Code:
[URL unfurl="true"]http://localhost/cgi-bin/junk.pl?name=joe&email=joe@here.com[/URL]
I get forwarded to the appropriate page.

I also did a syntax check on your email sub routine and
the syntax is OK.

So, there must be something else going on.

Questions:
Are you sure that the first #! line is correct for the web
server machine?

I've never played with Perl on a Mac. If you are running
OS X you may need to set an execute bit so the web server
can run the program.???? I don't know.

Are you sure you have sendmail where you say it is,
/usr/sbin/sendmail on the web server machine.

Are you writing and running the code on the same machine?

Can you make a trivial CGI run on that machine?

A trivial CGI,
Code:
#!perl
print qq(Content-type: text/html\n\n<html><body>The CGI Ran</body></html>);


The code as I ran it,
Code:
#!perl
use strict;
use CGI;
my $query = new CGI;

# retrieve the information submitted on the form
# ' No reason to pre-declare your vars, just put
# the 'my' in when you create them.
my $name             = $query->param('name');
my $contact         = $query->param('contact');
my $leavingfrom     = $query->param('leavingfrom');
my $destination     = $query->param('destination');
my $timeofday         = $query->param('timeofday');
my $month             = $query->param('month');
my $day             = $query->param('day');
my @year             = $query->param('year');
my $numberofpeople    = $query->param('numberofpeople');
my $emailaddress     = $query->param('emailaddress');
my $comments         = $query->param('comments');
my $email             = $query->param('email');
# construct some test that indicates a reasonable form
# submission... something like make sure you got
# a name and reasonably well-formed email address
# if you did then send the email and redirect to the
# thankyou page.  If you did not get a name and email,
# Print a complaint with some instructions.
if (($name =~ /\w/) && ($email =~ /.*?@.*?\..*/))
    {
    &send_the_email; # send email
    &redirect('thanks'); # call redirect sub for thanks
    }
else {
    &redirect('ooops'); # Gripe about poorly filled form.
    }
exit;
# ------------
#          SUBS
#-----------------------------------------------
sub redirect
    {
    my $mode = shift;
    if ($mode eq 'thanks') # name and email where OK
        {   
   print $query->redirect ('[URL unfurl="true"]http://www.panambus.com/thankyou.html');[/URL]
        }
    elsif ($mode eq 'ooops') # name or email weren't OK
        {
        print $query->header,
            $query->start_html,
            qq(<p>Incomplete submission.  Please go back and
                complete all data fields.</p>),
            $query->end_html;
        }
    }

#-----------------------------------------------
sub send_the_email
    {
    open(OPF,&quot;>junk.log&quot;) or die &quot;Failed OPF, $!\n&quot;;
    print OPF &quot;Send the mail\n&quot;;
    close OPF;
    }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Answer to goBoating
Am using MacOS 9.1 (not X)
Server is not my own but my ISP's here is the info they sent me:

Your CGI Path Information:
-----------------------
Path to Perl:

/usr/local/bin/perl

Path to Sendmail:

/usr/sbin/sendmail

Path to home directory:

/home/panambus

Path to Date:

/bin/date

What Operating System are we using?

RedHat Linux

What Web Server Software is running on the server?

Apache 1.3

Next, I tested your &quot;trivial CGI&quot; and it works. Of course it works - my original script (see top of the thread) is much more a complicated one and it uses sendmail and it still works so I dunno what to do except some [cry]ing and [curse]ing for now. Please help! Thanx
 
Dear goBoating !
Just spoke to my ISP people, had them take a closer look, they pinpointed the error to this line :

if (($name =~ /\w/) && ($email =~ /.*?@.*?\..*/))

They say the error is an
Unrecognized character \xA0
at that line
Whatever that means!
If you figure it out let me know please THANX.
 
OK.
Let's divide and conquer.
Let's just make this thing either gripe about
an incomplete form or redirect the browser to some other
page (panambus.com is refusing my http request).
Once we get that working, we can add the other stuff
back in.

This is about as simple as we can get.
It should either gripe about not getting a name or
redirect you to
try hitting it like
Code:
[URL unfurl="true"]http://localhost/cgi-bin/junk.cgi[/URL]
and
Code:
[URL unfurl="true"]http://localhost/cgi-bin/junk.cgi?name=joe[/URL]

Code:
#!perl
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';
 
my $query = new CGI;
my $name  = $query->param('name');

if ($name =~ /\w/)
  {
  print $query->redirect ('[URL unfurl="true"]http://www.csc.noaa.gov');[/URL]
  }
else 
  {
  print $query->header,
        $query->start_html,
        qq(<p>Incomplete submission.  Please go back and
           complete all data fields.</p>),
        $query->end_html;
  }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Hi goBoating!
Was out the office for a couple of days (went boating!)
Any way, all I got to work was this:

#!/usr/local/bin/perl
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';
my $query = new CGI;
print $query->redirect ('
At which point it does redirect to that page.


Then, even if I added just this line:

my $name  = $query->param('name');

I get the Internal Server Error.

Where on earth am I wrong???
 
OK! FINALY GOT THIS THING TO WORK
Hey goBoating! - I used some of your stuff such as the line:

use CGI::Carp 'fatalsToBrowser';

and:

my $query = new CGI

but not all of what you wrote.
Doesn't gripe about wrongly filled out form though, but then I didn't really need it to do that in the first place.
I wanna thank you all again for your time. So Long![wavey3]

This is the final script:

#! /usr/local/bin/perl
use strict;
use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
my $name; my $contact; my $leavingfrom; my $destination; my $timeofday; my $email;
my $month; my $day; my @year; my $numberofpeople; my $emailaddress; my $comments; my $query;
my $query = new CGI; my $rettimeofday; my $retmonth; my $retday;
$name = param('name');
$contact = param('contact');
$leavingfrom = param('leavingfrom');
$destination = param('destination');
$timeofday = param('timeofday');
$rettimeofday = param('rettimeofday');
$month = param('month');
$retmonth = param('retmonth');
$day = param('day');
$retday = param('retday');
@year = param('year');
$numberofpeople = param('numberofpeople');
$emailaddress = param('emailaddress');
$comments = param('comments');
$email = param();
open (MAIL, &quot;|/usr/sbin/sendmail -t&quot;)
|| Eror ('open', 'mail program');
print MAIL &quot;To: panamreserve\@hotmail.com\nFrom: $name\nSubject: Online Reservation for $month, $day, @year\n
<html><head><title>RESERVATION</title></head><body>
<h2>Reservation Information</h2>\n<b>Name: </b>$name\n
<p><b>Contact Telephone Number: </b>$contact\n
<p><b>Email Address: </b>$emailaddress\n
<p><b>Number of people in group: </b>$numberofpeople
<p><b>Leaving from: </b>$leavingfrom
<p><b>Destination: </b>$destination
<p><b>Date of travel: </b>$month, $day, @year, in the $timeofday
<p><b>Returning Date: </b>$retmonth, $retday, in the $rettimeofday
<p><b>Additional Comments:\n<p></b>$comments
</body></html>&quot;;
close (MAIL);
print $query->redirect ('
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top