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 scripts and Forms - Interaction? 2

Status
Not open for further replies.

JennyW

Technical User
Mar 1, 2001
323
CA
Hi,

I’m now able to test very basic .cgi scripts (e.g. hello world) with ease.

My new problem is getting my .cgi scripts to interact with <html> Forms. So far I’ve been unsuccessful.

I used goboating’s basic .cgi FAQ (faq452-653).
(Just so you know, I’m testing my CGI scripts with Apache 1.3.19 on Windows 98. I also have Perl on my system).

1. Here’s the html Form I used…
[tt]
<HTML>
<HEAD><TITLE>Most Basic CGI Input Page</TITLE></HEAD>
<BODY>
<FORM ACTION=&quot; c:/program files/apache/apache group/cgi-bin/emailtest.cgi &quot; METHOD=&quot;POST&quot;>
<P>email, please:
<INPUT TYPE=&quot;TEXT&quot; WIDTH=&quot;25&quot; NAME=&quot;Email_Address&quot;>
</P></FORM></BODY></HTML>
[/tt]

I think part of the problem may lie in the following line…
[tt]
<FORM ACTION=&quot; c:/program files/apache/apache group/cgi-bin/emailtest.cgi &quot; METHOD=&quot;POST&quot;>
[/tt]
I have spaces in between the words program files and apache group. Could this be part of the reason that my scripts aren’t running?

2. Here’s the .cgi code I used…
[tt]
#!c:/perl/bin/bin/perl
use CGI;
$cgi = new CGI;
$this_prog = $cgi->self_url;

print $cgi->header,$cgi->start_html(-title=>&quot;A Simple CGI Input Page&quot;);

my $email = $cgi->param('email');
if ($email)
{
# check to see if it looks like an email.
unless ($email =~ /.*@.*\.\w+/)
{ &killme(&quot;Not a valid email address&quot;); }
print &quot;<P>Email is $email</P>\n&quot;;
print $q->end_html;
}
else
{ # no email submitted, we must need to send the input page.
print $cgi->center,
$cgi->h3($header),
$cgi->start_form(-method=>'POST', -action=>&quot;$this_prog&quot;),
&quot;Your email address: &quot;,
$cgi->textfield(-name=>'email',
-size=>'25',
-maxlength=>'40'),
' ',$cgi->submit(-name=>'Submit', -value=>'submit'),
$cgi->endform,&quot;</center>&quot;;
}
sub killme
{
print &quot;<P>$_[0]</P>&quot;;
print $q->end_html;
die;
}
[/tt]
I’m lost. Isn’t this Form suppose to email me the email address entered in the Form?
Where do I specify my email address?

Thanks for reading,
Jenny
 
Yes, you are right with your [tt]<form>[/tt] tag being messed. You don't have to include all that other stuff.

Just have the name of the cgi script and place the html file within the same directory as the cgi file.

I don't have enough time to check the cgi now, but try changing that first.

Hope this helps,

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Hi Vic and others!

First, please disregard the .cgi script posted above. I couldn’t get that one working so I used another one of goboating’s examples.

1. Ok, in my <html> Form I changed this line…

<FORM ACTION=&quot; c:/program files/apache/apache group/cgi-bin/emailtest.cgi &quot; METHOD=&quot;POST&quot;>

to this…

<FORM ACTION=&quot;emailtest.cgi &quot; METHOD=&quot;POST&quot;>

But before I move on I was just wondering why goboating’s FAQ directed me to enter…

<FORM ACTION=&quot;/cgi-bin/simple.cgi&quot; METHOD=&quot;POST&quot;>

It’s a little different. Why is that?

2. Anyway, I tested my <html> Form with the following .cgi code. My results were strange.
(The .cgi script below is suppose to accept any email address and display it on a new page).
[tt]
#!/usr/local/bin/perl
#
%FORM = &cgidecode();
$email = $FORM{Email_Address};
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<HTML><HEAD><TITLE>Catching an email</TITLE></HEAD>\n&quot;;
print &quot;<BODY><P>Address is: $email.</P></BODY></HTML>&quot;;

sub cgidecode
{
local(%vars, $val, $key, $last, $buffer, $pair, @pairs);
# Checking the form method (GET or POST) used
# in the HTML code. POST method sends data to
# standard input, but GET adds it to the URL
# and stores it in QUERY_STRING.
if ($ENV{'REQUEST_METHOD'} eq &quot;POST&quot;)
{ read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
else
{ $buffer = $ENV{'QUERY_STRING'}; }

# Splitting up the data fields and store in array @pairs,
# they are seperated with &
@pairs = split(/&/, $buffer);

# Splitting the variable names and the values and storing
# them in the assoc. array %vars
foreach $pair (@pairs)
{
($key, $val) = split(/=/, $pair);
$val =~ s/\+/ /g;
$val =~ s/%(..)/pack(&quot;C&quot;,hex($1))/eg;
if ($key eq $last) {$vars{$key} .= &quot; &quot;.$val; }
else { $vars{$key} = $val; }
$last = $key;
}
return(%vars);
}
[/tt]

After I entered an email address I was brought to a new screen, which displayed the following…
[tt]

#!c:/perl/bin/bin/perl # %FORM = &cgidecode(); $email = $FORM{Email_Address}; print &quot;Content-type: text/html\n\n&quot;; print &quot;\n&quot;; print &quot;
Address is: $email.
&quot;; sub cgidecode { local(%vars, $val, $key, $last, $buffer, $pair, @pairs); # Checking the form method (GET or POST) used # in the HTML code. POST method sends data to # standard input, but GET adds it to the URL # and stores it in QUERY_STRING. if ($ENV{'REQUEST_METHOD'} eq &quot;POST&quot;) { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Splitting up the data fields and store in array @pairs, # they are seperated with & @pairs = split(/&/, $buffer); # Splitting the variable names and the values and storing # them in the assoc. array %vars foreach $pair (@pairs) { ($key, $val) = split(/=/, $pair); $val =~ s/\+/ /g; $val =~ s/%(..)/pack(&quot;C&quot;,hex($1))/eg; if ($key eq $last) {$vars{$key} .= &quot; &quot;.$val; } else { $vars{$key} = $val; } $last = $key; } return(%vars); }
[/tt]

Does anyone know what could have happened here?

Thanks,
Jenny
 
That means that Apache isn't sending the script to the Perl interpreter. Instead, it's simply displaying the original code as though it were Content-type: text/html (the reason for the messed up spacing is that HTML parsing replaces all multiple spaces with a single space).

You're obviously accessing the correct Perl code, so there's nothing wrong with your FORM action.

Make sure you've got the extension on your file correct (.cgi, I presume) and you might want to change the &quot;shebang&quot; line to reflect your Windows environment -- i.e. #!c:\perl\bin\bin\perl

Hope this helps,

brendanc@icehouse.net
 
Hi,
I tried my .cgi script with my shebang line as...
[tt]
#!c:\perl\bin\bin\perl
[/tt]
and I'm getting the same results.

Also, my scripts are saved as .cgi files.

How can I make sure that Apache is sending the script to the Perl interpreter?

Thanks,
Jenny
 
Try taking that extra \bin out of there, and try #!c:\perl\bin\perl.

That should do it unless you have changed where the perl interpreter is, but its default on windows machines is C:\perl\bin\perl.exe

Hope this helps.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Hi Vic and others.

No, my perl.exe file is in my second /bin folder.

[tt]#!c:\perl\bin\bin\perl[/tt]

...is the location of perl.exe on my machine.

Still trying to figure out what the problem could be.

Thanks,
Jenny
 
The only other thing that I could think of is changing th back slashes to foward slashes. Thats how they are on *nix machines, so I would only assume that it would work with Apache (being open source and all).

Try that, if that doesn't work, I'm spent.

Hope this helps,
-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
The reason the slashes are forward slashes on UNIX/Linux machines is because that's how the filesystem is structured. It has nothing to do with Apache or Perl ... that line is actually parsed before the script is sent to the interpreter. Therefore, on Windows machines, the slashes should be backslashes as that is how the Win filesystem operates.

Regardless, it doesn't appear that that's the problem at all. It's one of the following:

The extension (.cgi) isn't mapped correctly in the Apache config. If other scripts with that extension work, eliminate this option.

The script is not in a directory configured with the ExecCGI option nor a ScriptAlias directory. If you can run other scripts from the same directory, eliminate this option.

The extension that you're viewing in Windows Explorer may not be the true extension Windows is reading. In Windows Explorer, go to the View menu -> Tools -> Folder Options. From there, select the View tab and uncheck the box labeled &quot;Hide extensions for known file types&quot;. Click OK, then browse to the folder your cgi scripts are in and verify that your script is not called something like &quot;simple.cgi.txt&quot;.

I can't think of anything else off hand, but I'll keep my fingers crossed that one of these options gets the issue sorted out.

Best of luck,

brendanc@icehouse.net
 
Hi guys!
Thanks for the responses!

Sophisticate, unfortunately I had to eliminate your 1st and 3rd ideas, they don’t apply.
I didn’t understand your 2nd possibility…

The script is not in a directory configured with the ExecCGI option nor a ScriptAlias directory. If you can run other scripts from the same directory, eliminate this option.

What does that mean?

Regardless, I stopped using that script. I tried another that was very similar and it worked fine.
Here’s a link to the .cgi that worked for me…

I’m having trouble with another script now.
It’s a .cgi script where the website visitor can email me using an <html> form.

I highlighted the segments that I think I’m making errors on.
Here’s part of the script. To see the whole script go to this link…

[tt]
#!c:\perl\bin\bin\perl.exe

# URL to go to if there is a problem with form input
$ErrorPage = &quot;a_fail.html&quot;;

# URL to go to when form has been successfully submitted
$ThankPage = &quot;a_thanks.html&quot;;

# URL to go to if a 'foreign' referer calls the script
$EvilReferer = &quot;

# E-mail address to send intake form to (your address)
# If not using PERL 5, escape the @ thus: \@ instead of @
$YourEmail = 'themustache@hotmail.com';

# Script works only on your server(s) - ('URL1','URL2')
@referers = ('
# Location of mail program - check your doc or ask admin
$MailProgram = '/usr/lib/sendmail';

# Subject of the e-mail autoreply to the submitter
$Subject = &quot;Thanks for Your Message!&quot;;

# Header line in the auto-reply message
$Header = &quot;GOOBERS UNLIMITED&quot;;

# Brief tail message for body of e-mail autoreply
$TailMessage = &quot;If your message requires a reply, we'll get back to you soon.&quot;;

# Your signature lines the end of the autoreply e-mail
$Signature1 = &quot;John Q. Public&quot;;
$Signature2 = &quot;[/tt]

I’m unsure what to enter in the highlighted segments? Is this where I’m making errors?

Thanks,
Jenny
 
Jenny:

&quot;The script is not in a directory configured with the ExecCGI option nor a ScriptAlias directory. If you can run other scripts from the same directory, eliminate this option.&quot;

This means that the .cgi script is not under a directory that is enabled to execute CGI scripts. The default is the cgi-bin directory.

What you need to do under your httpd.conf file is to locate where it talks about ExecCGI and make sure that it is explicitly named. Although it doesn't say it needs to be, I think that it should so that you know its being named.

You also need to set your script alias directory to where you will have the .cgi files located. I would suggest just keeping it /cgi-bin/.

I think thats all.

Hope this helps.

-Vic
vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Hi Vic and others.
Thanks for the explanation.

I already have my ExecCGI field set up in my [tt]httpd.conf[/tt] file and my scripts have always been in my Apache cgi-bin folder.

I guess that emliminates the possibility of Sophisticate's 2nd possibility.

If any of you guys get the time can you check my [tt].cgi[/tt] code above in my previous post?

Thanks,
Jenny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top