Printing Contents of Array in External File to Browser
I am trying to write the contents of an external file onto the web browser. The ultimate goal is to compare the entires in the text file to entries submitted through a form. If they match, perform an operation. When I run the perl command "perl -w submit.pl" in my command prompt, it works correctly. However, when I try to run it in a web browser, nothing displays. I have tried everything I can think of and can't get it working. I would appreciate some help. Thanks a lot.
Code:
# Standard header stuff
use CGI qw( :standard );
use strict;
print( header() );
print( start_html() );
checkFraud();
sub checkFraud {
my $filename = "../fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}
print ( end_html() );
# Standard header stuff
use CGI qw( :standard );
use strict;
sub checkFraud {
my $filename = "../fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}
print( header() );
print( start_html() );
checkFraud();
print ( end_html() );
or
Code:
# Standard header stuff
use CGI qw( :standard );
use strict;
print( header() );
print( start_html() );
&checkFraud();
print ( end_html() );
sub checkFraud {
my $filename = "../fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}
you don't need to use print so many times. This will also work:
Thanks for the help...Unfortunately, I am still getting the same results (nothing is displayed in the web browser, but it works fine in the command prompt)
Below is the complete perl file (I have a few other functions declared, but all of them function FINE, which is why I didn't originally include them...)
Thanks again for the continued help!
Code:
# Standard header stuff
use CGI qw( :standard );
use strict;
# Get inputs
my $name = param("name");
my $phone = param("phone");
my $email = param("email");
my $address = param("streetAddress");
my $city = param("city");
my $state = param("state");
my $zip = param("zip");
my $passwd = param("passwd");
my @events = param("events");
my $firstClub = param("firstClub");
my $comments = param("experience");
my $years = param("years");
my $validInput = 1;
#validateInfo();
#if($validInput == 1) {
# printHeader();
#}
#checkFraud();
sub validateInfo {
my $message1 = "";
my $message2 = "";
my $message3 = "";
if(($name eq "") ) {
$message1 = "You must enter a name!";
$validInput = 0;
}
if(!($email =~ /^\w.+\@\w+(\.org|\.net|\.com)$/i )){
$message2 = "You must enter a valid email addres (Eg. home\@home.net or home\@home.com or home\@home.org) ";
$validInput = 0;
}
if((!@events)) {
$message3 = "You must select at least ONE event";
$validInput = 0;
}
if(!$validInput) {
print h1("$message1" . "<br />" . "$message2" . "<br />" . "$message3");
print ("Click <a href='http://www.m111662.it350.cs.usna.edu/Lab08/register.html'>HERE</a> to go back and make the appropriate corrections <br />");
}
}
sub printHeader {
print h1("Registration Submitted!");
print ("Below are the details of your submission. Thanks a lot! <br />");
print ("<br />");
print ("Name: $name <br />");
print ("Phone: $phone <br />");
print ("Email: $email <br />");
print ("Address: $address <br />");
print ("City: $city <br />");
print ("State: $state <br />");
print ("Zip: $zip <br />");
print ("Password: $passwd <br />");
print ("Events: @events <br />");
print ("Is this your first time to be a member of a running club?: $firstClub <br />");
print ("Experience: $comments <br />");
print ("Years of running experience: $years <br />");
}
sub checkFraud {
my $filename = "../fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}
print( header() );
print( start_html() );
checkFraud();
print ( end_html() );
The text file is in the same directory as the submit.pl script. Below is the exact code that is in the perl script. Thanks for all of the continued help!
Code:
#!/usr/local/bin/perl
# Standard header stuff
use CGI qw( :standard );
use strict;
use CGI::Carp qw(fatalsToBrowser);
# Get inputs
my $name = param("name");
my $phone = param("phone");
my $email = param("email");
my $address = param("streetAddress");
my $city = param("city");
my $state = param("state");
my $zip = param("zip");
my $passwd = param("passwd");
my @events = param("events");
my $firstClub = param("firstClub");
my $comments = param("experience");
my $years = param("years");
my $validInput = 1;
#validateInfo();
#if($validInput == 1) {
# printHeader();
#}
#checkFraud();
sub validateInfo {
my $message1 = "";
my $message2 = "";
my $message3 = "";
if(($name eq "") ) {
$message1 = "You must enter a name!";
$validInput = 0;
}
if(!($email =~ /^\w.+\@\w+(\.org|\.net|\.com)$/i )){
$message2 = "You must enter a valid email addres (Eg. home\@home.net or home\@home.com or home\@home.org) ";
$validInput = 0;
}
if((!@events)) {
$message3 = "You must select at least ONE event";
$validInput = 0;
}
if(!$validInput) {
print h1("$message1" . "<br />" . "$message2" . "<br />" . "$message3");
print ("Click <a href='http://www.m111662.it350.cs.usna.edu/Lab08/register.html'>HERE</a> to go back and make the appropriate corrections <br />");
}
}
sub printHeader {
print h1("Registration Submitted!");
print ("Below are the details of your submission. Thanks a lot! <br />");
print ("<br />");
print ("Name: $name <br />");
print ("Phone: $phone <br />");
print ("Email: $email <br />");
print ("Address: $address <br />");
print ("City: $city <br />");
print ("State: $state <br />");
print ("Zip: $zip <br />");
print ("Password: $passwd <br />");
print ("Events: @events <br />");
print ("Is this your first time to be a member of a running club?: $firstClub <br />");
print ("Experience: $comments <br />");
print ("Years of running experience: $years <br />");
}
sub checkFraud {
my $filename = "fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
print ("$currentLine");
}
close(INPUT_FILE);
}
print( header() );
print( start_html() );
checkFraud();
print ( end_html() );
Since you're using inetpub, it would appear that you're using IIS, which I haven't used for a long time. And, since this looks to be a homework assignment, we won't be able to provide a complete answer. However, we still can give you a little guidance.
Thanks for the info. This is actually a lab and we are allowed to use any sources available to us. I figured it was a "path" issue, but if I do not know the directory structure, how am I going to insert the full path to the .txt file?
Well, you stated that the text file is in the same directory as the script, so in what directory did you put the script. BIG hint, look at your error message.
The text file IS in the same directory as the script...I am wondering why it is adding the "www.m111662.it350.cs.usna.edu\Lab08\submit.pl" onto the path "d:\inetpub\it350\"...That is where I think the problem is...It is using both a full path in addition to the web site address...That is what the error message says...
Shouldn't this code match the value in $name to the currently entry being examined from the file?
Code:
sub checkFraud {
my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
if($currentLine eq $name) {
print ("FRAUD!");
}
}
close(INPUT_FILE);
}
Shouldn't this code match the value in $name to the currently entry being examined from the file?
Code:
sub checkFraud {
my $filename = "d:/inetpub/it350/www.m111662.it350.cs.usna.edu/fraud-values.txt";
open(INPUT_FILE, $filename)
or die "Couldn't open $filename";
while (<INPUT_FILE>) {
my $currentLine = $_;
if($currentLine eq $name) {
print ("FRAUD!");
}
}
close(INPUT_FILE);
}
depend if $name is defined somewhere and what is inside.