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

script not workin

Status
Not open for further replies.

WintersMystic

Programmer
Aug 8, 2001
39
US
hi, :)

ok, this script is a contact form email script. i havent put in the email routine, cause that i know how to do. well, this one wont work at all. what im wanting it to do so far is show each of the error msgs if that field is left blank. ive never used a die call, so i dont know if thats the reason its failing or not. ive been writing in perl for a few months now. anyone help?

Code:
usr/bin/perl
use CGI::Carp qw(fatalsToBrowser); 
BEGIN {
     use CGI::Carp qw(carpout);
     open(LOG, ">mycgi-log") or
       dienice("Unable to open mycgi-log: $!\n");
     carpout(LOG);
   };
#contact us form#

#variables#

use CGI ':standard';
if(param()){
$Name=param('name') or dienice("Please enter a name");
$Ref=param('ref') or dienice("Please specify a referer");
$Type=param('type_comment') or dienice("Please specify a comment type");
$Msg=param('msg') or dienice("Please enter some comments");
$Email=param('email') or dienice("Please enter a valid email address");
} else {
print &quot;<html><head><title>Result</title>&quot;;
print &quot;<body>&quot;;
print &quot;$Name<p>&quot;;
print &quot;$Email<p>&quot;;
print &quot;$Ref<p>&quot;;
print &quot;$Type<p>&quot;;
print &quot;$Msg<p>&quot;;
print &quot;</body></html>&quot;;
}
$ZS=&quot;wave\@zip-script.com,mysticpoet\@zip-script.com&quot;;
#end variables#

sub dienice{
my ($msg) = @_;
print &quot;<html>&quot;;
print &quot;<body>&quot;;
print &quot;<h2>Error</h2>\n&quot;;
print &quot;$msg&quot;;
print &quot;</body>&quot;;
print &quot;</html>&quot;;
exit;
}
 
I think the first line should be:

#!/usr/bin/perl

----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
Age: 16
E-mail: projectnet01@cs.com
Company:(not done yet) :)
-Aaron
----------------------------------------
 
Aaron:

yep, i have #! in there, just forgot to get it when i CCPd the code :( sorry bout that.
 
WintersMystic

A couple of things.

(1) It looks like the logic for your code may be wrong.

Basically you have:

Code:
if( param() ) {
    check parameters
} else {
    print parameters
}

Hence the parameters printed will always be empty.

(2) A CGI script must output header information according to the HTTP protocol. In other words, you would need to have:

Code:
print &quot;Content-type: text/html\n\n&quot;;

Before any other printed text.

Note that since you are using the CGI package, you can use several convenience functions like:

Code:
print header, start_html;
... print parameters here ...
print end_html;

A very simple script would look like:

Code:
use CGI qw( :all );
use CGI qw( fatalsToBrowser );

print header, start_html;

if( param ) {
    my $name = param( 'name' ) || &quot;guest&quot;;
    my $age  = param( 'age' )  || &quot;&quot;;

    print h1( &quot;Hello $name&quot; );
    if( $age ) {
        print h2( &quot;You claim to be $age years old&quot; );
    }
}
else {
    print start_form;
    print text_field( -name => 'name' );
    print br;
    print text_field( -name => 'age' );
    print end_form;
}

print end_html;

Note, I have just typed this in from memory, so no guarantees that it will run bug free :-( (for example, the text_field function may be textfield?). Try perldoc CGI for more information on the CGI package.

Cheers, NEIL
 
PS:

You might want to add a submit button somewhere ;-) using:

Code:
print submit;

Neil
 
toolkit:

sorry for being a pain. i am now calling the script from an HTML page. :)

your suggestions helped me tremendously. but one problem still. everything works but the error routine. it works only when i include print header; or print &quot;Content-type: text/html\n\n&quot;;

but then it prints out the error msg PLUS Content-type. any suggestions on that?

Code:
#!/usr/bin/perl
use CGI qw(:all);
use CGI qw(fatalsToBrowser);

$Name=param('name') or died(&quot;Name Empty&quot;);
$Ref=param('ref') or died(&quot;Ref Empty&quot;);
$Type=param('type_comment') or died(&quot;Type Empty&quot;);
$Msgs=param('msg') or died(&quot;Msgs Empty&quot;);
$Email=param('email') or died(&quot;Email Empty&quot;);
$ZS=&quot;wave\@zip-script.com,mysticpoet\@zip-script.com&quot;;

print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><title>HI</title>&quot;;
print &quot;<body>&quot;;
print &quot;Name:$Name<br>Email:$Email<br>Type:$Type<br>Ref:$Ref<br>Message:$Msgs<br>ZS:$ZS&quot;;
print &quot;</body></html>&quot;;

sub died{
($msg) = @_;
print start_html;
print &quot;$msg&quot;;
print end_html;
}

this version returns a 505 error, but when i put the header in, it works, just printing out the header plus the error. :(
 
WintersMystic

I made an error in my first script - the code:

Code:
use CGI qw( fatalsToBrowser );

Should have been:

Code:
use CGI::Carp qw( fatalsToBrowser );

This overrides the die subroutine, so they are reported to the browser. So you can make your code look like:

Code:
#!perl
use CGI qw( :all );
use CGI::Carp qw( fatalsToBrowser );

$Name  = param( 'name' ) or die &quot;Name empty\n&quot;;
$Ref   = param( 'ref' )  or die &quot;Ref empty\n&quot;;
$Type  = param( 'type_comment' ) or die &quot;Type empty\n&quot;;
$Msgs  = param( 'msgs' )  or die &quot;Msgs empty\n&quot;;
$Email = param( 'email' ) or die &quot;Email empty\n&quot;;
$ZS = &quot;wave\@zip-script.com,mysticpoet\@zip-script.com&quot;;

print header, start_html( -title => 'HI' );

print qq[
Name:    $Name<br>
Email:   $Email<br>
Type:    $Type<br>
Ref:     $Ref<br>
Message: $Msgs<br>
ZS:      $ZS
];

print end_html;

Cheers, NEIL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top