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!

Perl form/post question 1

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
The first PERL program that I want to write solo will allow my students to answer questions over the web via a form/post method. Getting the below book example program to work is sort of fundamental to writing my student submittal program (I know programs most likely already exist that allow students to submit answers to a teacher over the internet...but I'd like to learn PERL and how to do it myself).<br>
<br>
The below file called &quot;write.cgi&quot; comes straight from a book on PERL5. The program, when combined with a HTML file called &quot;write.html&quot; should allow a web user to enter in some data in various fields, submit the date to ther web server, write the data to a file called 1.html located in a subfolder called 'numbers' located in the folder called 'cgi-bin', and then allow the web user to review the data submitted. <br>
<br>
File &quot;write.cgi&quot; is located in a folder called &quot;cgi-bin&quot;. I've confirmed that the file is functional by using the command 'perl write.cgi' which works great. It actually creates a file called 1.html in the 'numbers' subdirectory.<br>
<br>
I've also confirmed that the file is being 'activated' via the web by renaming the file &quot;write.cgi&quot; to &quot;write1.cgi&quot;. When I do this I get the 404 file not found html error when I hit the submit key. When the file is called &quot;write.cgi&quot; I don't get a 404 file not found error.<br>
<br>
The web server is a unix system.<br>
write.cgi permissions are set to 755<br>
write.html permissions are set to 744<br>
numbers folder within cgi-bin is set to 755<br>
<br>
Now for the problem. When I access the html file &quot;write.html&quot; from the web I get the form as expected. When I fill in the form and hit the submit button I get the next html screen that allows me to select the file &quot;1.html&quot; which should allow me to go read the file that was created by the &quot;write.cgi&quot; script called &quot;1.html&quot;. When I select &quot;1.html&quot; on the web browser, I get a 404 error: file doesn't exist.<br>
<br>
I've checked via telnet and the file 1.html was never created/written on the server.<br>
<br>
Sooo, the error appears to be that I'm not able to write the data on the form via the post method to a file on the server side.<br>
<br>
Can anyone help me here. <br>
<br>
FYI: I have worked this one for awhile. I've double checked the permissions on the folders, etc.<br>
<br>
Thanks again...and below are the two files discussed above.<br>
<br>
Jerry<br>
<br>
<br>
-----&quot;write.html&quot; is shown below without any changes from the book-----------<br>
<br>
<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
&lt;title&gt;Write.cgi&lt;/title&gt;<br>
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;&gt;<br>
&lt;/head&gt;<br>
<br>
&lt;body bgcolor=&quot;#FFFFFF&quot;&gt;<br>
&lt;form method=&quot;POST&quot; action=&quot;cgi-bin/write.cgi&quot;&gt;<br>
&lt;p&gt;Title:&lt;br&gt;<br>
&lt;input type=&quot;text&quot; name=&quot;Student Name&quot; size&quot;20&quot;&gt;<br>
&lt;/p&gt;<br>
&lt;p&gt;Heading:&lt;br&gt;<br>
&lt;input type=&quot;text&quot; name=&quot;Date&quot; size=&quot;20&quot;&gt;<br>
&lt;/p&gt;<br>
&lt;p&gt;Body:&lt;br&gt;<br>
&lt;textarea rows=&quot;4&quot; name=&quot;Answer&quot; cols=&quot;20&quot; wrap=&quot;virtual&quot;&gt;&lt;/textarea&gt;<br>
&lt;/p&gt;<br>
&lt;p&gt;<br>
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;submit&quot;&gt;<br>
&lt;input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;reset&quot;&gt;<br>
&lt;/p&gt;<br>
&lt;/form&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>
<br>
<br>
---&quot;write.cgi&quot; is shown below without any changes fromt the book I'm using to learn PERL------<br>
<br>
#!/usr/bin/perl<br>
&get_form_data;<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
opendir(DIR, &quot;./numbers&quot;);<br>
while($name = readdir(DIR))<br>
{<br>
next if $name !~ /^\d*.html/;<br>
push(@files, $name);<br>
}<br>
close(DIR);<br>
if($#files == 0)<br>
{<br>
$nextfile = &quot;1.html&quot;;<br>
}<br>
<br>
else<br>
{<br>
<br>
$lastfile = $files[$#files];<br>
$lastfile =~ s/.html//g;<br>
$nextfile = $lastfile + 1;<br>
$nextfile .= &quot;.html&quot;;<br>
}<br>
<br>
open(OUT, &quot;&gt;numbers/$nextfile&quot;);<br>
print OUT &quot;&lt;HTML&gt;\n&lt;HEAD&gt;\n &quot;;<br>
print OUT &quot;&lt;TITLE&gt;\n&quot;;<br>
print OUT &quot;$FORM{'title'}\n&quot;;<br>
print OUT &quot;&lt;/TITLE&gt;\n&quot;;<br>
print OUT &quot;&lt;/HEAD&gt;\n&quot;;<br>
print OUT &quot;&lt;BODY BGCOLOR=\&quot;#FFFFFF\&quot;&gt;\n&quot;;<br>
print OUT &quot;&lt;H1&gt;\n&quot;;<br>
print OUT &quot;$FORM{'heading'}\n&quot;;<br>
print OUT &quot;&lt;/H1&gt;\n&quot;;<br>
print OUT &quot;&lt;P&gt;\n&quot;;<br>
print OUT &quot;$FORM{'body'}\n&quot;;<br>
close(OUT);<br>
push(@files, $nextfile);<br>
print &quot;&lt;HTML&gt;\n&lt;BODY&gt;\n&quot;;<br>
foreach $file (@files)<br>
{<br>
print &quot;&lt;A HREF=\&quot;numbers/$file\&quot;&gt;$file&lt;/A&gt;\n&quot;;<br>
print &quot;&lt;BR&gt;\n&quot;;<br>
}<br>
print &quot;&lt;/BODY&gt;\n&lt;/HTML&gt;\n&quot;;<br>
exit;<br>
<br>
sub get_form_data<br>
{<br>
# Get the input<br>
read(STDIN, $buffer, $ENV{ 'CONTENT_LENGTH' } );<br>
# Split the name-value pairs<br>
@pairs = split(/&/, $buffer);<br>
foreach $pair (@pairs)<br>
{<br>
($name, $value) = split(/=/, $pair);<br>
# Un-Webify plus signs and %-encoding<br>
$value =~ tr/+/ /;<br>
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;<br>
$value =~ s/&lt;!--(.¦\n)*--&gt;//g;<br>
<br>
$FORM{ $name } = $value;<br>
}<br>
}<br>

 
I cut and pasted the code into my htdocs and cgi-bin dirs, changed the 'action' in write.html to my cgi-bin dir, chmod +x on write.cgi, created the 'numbers' dir. and the code ran. There are few wrinkles with the way it runs, but that is not your problem (yet).<br>
<br>
?Who owns the numbers directory? If you do (and you are not the httpd daemon), and you set the permissions to 755, then the httpd daemon ( any web server process) can not write to that dir. Try opening the dir up to 777 just to see if this is the problem. If it is, decide to leave it that way or chown on the dir to 'httpd' and set the permissions to 755 or whatever you like to remedy the problem. <br>
<br>
Another view (of maybe the same problem)......<br>
There is no error checking in the example from the book. Assuming a plain vanilla web server 'htdocs' directory and 'cgi-bin' directory, my suspicion ( is that spelled correctly?) is that you are not successfully opening your output file. Try adding a little simple error checking to the cgi......like this.....<br>
<br>
<br>
'open(OUT, "&gt;numbers/$nextfile") ¦¦ print "Failed to open $nextFile, $! &lt;BR&gt;\n";'<br>
<br>
'"$!" - contains the most recent error. If the 'open' did not work, "$!" will show you what the OS said about the problem.'<br>
<br>
If the 'open' fails, and since you have already printed the Content header to the browser, you should get the error statement in the resulting html page.<br>

 
Thank you! Your suggestion worked great, especially the description of how to do some simply error detection in the script.<br>
<br>
Before changing the permissions on the file directory I inserted the error detection logic into the script. As suspected the error was due to file protection/access. I then changed the permission for the numbers directory and everything worked out.<br>
<br>
I will use the error detection logic in future code.<br>
<br>
Thanks again.<br>
<br>
Jerry
 
Jerry,<br>
Here is a bit of code you can look at or even use that I place in just about all of my scripts. This will give you a neat output page for your errors and even some extra info that might make errors easier to track. You can call this sub like so:<br>
<br>
open(FILE, &quot;myfile.html&quot;) ¦¦ &error(&quot;$!&quot;);<br>
<br>
Yes I know it is a bit much but it helps track errors even when you are not the one running the script. Hope this helps.<br>
<br>
$debug = 1; #make it zero if you don't want the env variables.<br>
$prog_name= 'My Program'; #name of the program<br>
$log = '/full/path/to/error/log/error.log'; #This is the full path to your error log.<br>
<br>
sub error {<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
# Displays any errors and prints out FORM and ENV info.<br>
($Second, $Minute, $Hour, $DayOfMonth, $Month, $Year, $Weekday,<br>
$DayOfYear, $IsDST) = localtime(time);<br>
<br>
$RealYear = $Year + 1900 if ($Year &lt;= 99);<br>
$RealYear = $Year + 2000 if ($Year &gt; 99);<br>
$Month++;<br>
if($Month &lt; 10) {$Month = &quot;0&quot; . $Month}<br>
if($DayOfMonth &lt; 10) {$DayOfMonth = &quot;0&quot; . $DayOfMonth}<br>
if($Hour &lt; 10) {$Hour = &quot;0&quot; . $Hour}<br>
if($Minute &lt; 10) {$Minute = &quot;0&quot; . $Minute}<br>
if($Second &lt; 10) {$Second = &quot;0&quot; . $Second}<br>
$date = &quot;$Hour:$Minute:$Second $Month-$DayOfMonth-$Year&quot;;<br>
open(LOGS, &quot;&gt;&gt;$log&quot;) ¦¦ print &quot;can't open log file&quot;; #use a print statement here otherwise you would end up in a never ending loop.<br>
print LOGS &quot;$prog_name¦$date¦$!&quot;;<br>
close(LOGS);<br>
<br>
print &quot;&lt;h3&gt;Error: $!&lt;/h3&gt;\n&quot;;<br>
print &quot;Message: $_[0]\n\n&quot;;<br>
print &quot;$_[1]\n\n$_[2]\n\n&quot;;<br>
if ($debug == 1) {<br>
print &quot;&lt;PRE&gt;\n Form Variables \n&quot;;<br>
foreach $key (sort keys %in) {<br>
print &quot;$key: \t$in{$key}\n&quot;;<br>
}<br>
print &quot;\n Environment Variables \n&quot;;<br>
foreach $env (sort keys %ENV) {<br>
print &quot;$env: \t$ENV{$env}\n&quot;;<br>
}<br>
}<br>
print &quot;\n&lt;/PRE&gt;&quot;;<br>
exit;<br>
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top