Click to See Complete Forum and Search --> : perl script to validate US towns


Orsen Cart
09-27-2004, 06:17 PM
Hi

Firstly, I posted a related topic on the Javascript board but perhaps I was in the wrong place. Apologies to anyone there who now reads this!

I think what I want to do should be incredibly simple but I have never written a line of Perl code in my life so my first attempt will need to be simple! I want to validate the content of a textbox against a list of items in a file on the server. In this case, the list of items is a list of US towns & cities. When I submit the form, this will pass a 'state' plus 'town' to the perl script which will then return true or false.

My questions really are
1) Where is a good place to start on the web to learn this stuff?
2) What would the perl code be to perform the above validation?
3) What would the list file look like?

I would imagine that there must be a pre-written Perl script on the web somehere to do this but when I look at any of the sites offering such scripts, they assume a level of knowledge which I don't have. The same goes for Perl forums too. If someone can point me directly to a script that can do this, that would be great.

Many thanks

Charles
09-27-2004, 07:36 PM
#!c:/perl/bin/perl.exe -w

use CGI::Pretty qw (:all);

my $city = param ('city');
my $state = param ('state');

my @maryland = qw(baltimore annapolis);
my @pennsylvania = qw(pittsburgh philadelphia);

my %states;
$states{maryland} = \@maryland;
$states{pennsylvania} = \@pennsylvania;

my $errorMessage = "I'm sorry, $city, $state appears to be incorrect.";

print header, start_html ({title => 'Wrong City'}), h1('Wrong City'), p($errorMessage), end_html # unless grep {/^$city$/} @{$states{$state}};

Orsen Cart
09-28-2004, 02:13 PM
Thanks Charles (again)

I can't claim to understand the code so it would be nice to understand what it is doing. I went looking for a book on Perl today but with no success. How much more difficult would it be to pass in a state and then return a pre-set list of towns into a select list on my page?

Thank for your continued help and patience.

Charles
09-28-2004, 06:15 PM
#!c:/perl/bin/perl.exe -w

use CGI::Pretty qw (:all);

my $state = param ('state');

my @maryland = qw(baltimore annapolis);
my @pennsylvania = qw(pittsburgh philadelphia);

my %states;
$states{maryland} = \@maryland;
$states{pennsylvania} = \@pennsylvania;

print header,
start_html ({title => "Select a city in $state"}),
start_form ({action => $0}),
fieldset(label("Select a city in $state")),
Select (option ($states{$state})),
end_form,
end_html;

Orsen Cart
09-28-2004, 06:54 PM
Many thanks Charles; I'll give it a go.

Orsen Cart
10-02-2004, 01:20 PM
Hi guys

I finally got around to trying this, after having bought myself a book on perl, having downloaded a perl course from the web and having read all of the advice given by my ISP host on using CGI in their environment. When I submit the HTML form, I am getting a '500' message. I have stripped the code right down and cannot get past this '500' message.

The HTML I have is ..


<form action="answer.cgi">
<select name="state">
<option value = "Yes">Yes</option>
<option value = "No">No</option>
</select>
<input type="submit" value="Go">
</form>


and the perl script I have is ...


#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
print "Got here";


According to the ISP, the cgi file can be resident anywhere. The interpreter path and the 'use' command are direct copies from thier own instructions. The permissions on the .cgi file are 0755 as stated in their instructions.

Despite all of this, I an still getting the '500' message and cannot progress. The url at the top of the message page is http://<url>/answer.cgi?state=Yes which looks to be correct.

Has anyone got any suggestions at to what may be causing this error? In the meantime, I'm going to try downloading apache and a perl interpreter to try to develop this locally, but I would still appreciate and ideas as to what scenario could be causing this error.

Many thanks

Orsen Cart
10-03-2004, 10:09 AM
Hi

I installed apache & perl on my pc. Apache seems to be working ok as when I access http://localhost/ I can see the web page that I loaded into the apache root htdocs directory.

However, when I run the following .pl file directly via http://localhost/cgi-bin/answer.pl

#!c:\perl\bin\perl.exe
use CGI;
$query = new CGI;
print $query->header;
print "<html><head><title>A test</title></head>\n";
print "<body>Success</body></html>";

This gives '500' error message, even when I include the line ...
use CGI::Carp qw(fatalsToBrowser);

If I try to run the .pl file from te command line using ...
c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl

.. I get ...

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "rtf1\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "ansi\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "ansicpg1252\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "deff0\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "f0\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "fswiss\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "fprq2\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "f1\"
Backslash found where operator expected at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "froman\"
syntax error at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "
rtf1\"
syntax error at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "
f1\"
syntax error at c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl line 1, near "
}}"
Execution of c:\Progra~1\ApacheGroup\Apache2\cgi-bin\answer.pl aborted due to co
mpilation errors.

I have no idea what this error text is telling me or how to put it right. This is getting beyond a joke for me as I have now spent in excess of 23 hours this weekend trying to get something working and I have not yet been able to execute the simplest of perl scripts.

Any help would be very gratefull recieved.
Many thanks

Charles
10-04-2004, 06:44 AM
I looks like you have some sort of problem with your installation of Apache, or Perl or GGI.pm and I'm not sure why you're going that route. If you're just trying to get the syntax working locally on Windows then use ActivePerl (http://www.activestate.com/Products/ActivePerl/).

And if you're going to use the CGI module, and you should, then use the CGI module.

#!c:/perl/bin/perl.exe -w

use strict;

use CGI::Pretty;
my $q = new CGI;

print $q->header,
$q->start_html ('Example'),
$q->h4('Success'),
$q->end_html;

Orsen Cart
10-04-2004, 04:49 PM
Hi Charles

The hardest errors to find are the simplest!! It appears that because I had used WordPad to create the .pl file, there were some extra characters there. I had thought that WordPad was a plain text editor just like NotePad. Now I know better.

In addition to that, the example I had used from my book did not work whereas yours does work!

Just for the record, I am using Active Perl.

Many thanks as always.

Orsen Cart
10-09-2004, 01:02 PM
Hi

Can anyone help a little further with this.

The html I have is ..
<form action="/cgi-bin/towns.cgi">
<select name="state">
<option value = "maryland">Maryland</option>
<option value = "pennsylvania">Pennsylvania</option>
</select>
<input type="submit" value="Go">
</form>

and the cgi script is ...
#!c:/perl/bin/perl.exe -w

use CGI::Pretty qw (:all);

my $state = param ('state');

my @maryland = qw(baltimore annapolis);
my @pennsylvania = qw(pittsburgh philadelphia);

my %states;
$states{maryland} = \@maryland;
$states{pennsylvania} = \@pennsylvania;

print header,
start_form ({action => $0}),
Select (option ($states{$state})),
end_form;

How can I get the select in the cgi script to appear on the first html page rather than the page that is created by the cgi script?

Also, is there any way that I can have a select hardcoded in the html page, but get the option values from a flat file which is held on the server rather than having the options hardcoded in the html file.?

Many thanks

CyCo
10-11-2004, 02:17 PM
Originally posted by Orsen Cart
Hi

Can anyone help a little further with this.

How can I get the select in the cgi script to appear on the first html page rather than the page that is created by the cgi script?

Also, is there any way that I can have a select hardcoded in the html page, but get the option values from a flat file which is held on the server rather than having the options hardcoded in the html file.?

Many thanks
Hi, Orsen Cart...
I've put together a little example (http://cyco.freeprohost.com/examples/ocform.htm) that may be similar to what you are trying to achieve.

Orsen Cart
10-12-2004, 03:06 AM
Hi CyCo

Thanks for your effort on this; it is very much appreciated.

I had tried using an iframe before but the problem I had was that the page being displayed as a result of the form submit was being displayed in that iframe, just as your 'thanx' message is. Is there any way of making this 'thanx' page repace the main html page rather than appear in the iframe?

Also, in the same way that I can put javascript in a seperate .js file and have it 'included' in the html page, can I include another html file in just the same way? I.e. can I reuse the same code as an 'include' rather than as a 'call'? I couldn't find anything in my O'Reilly book!

Many thanks

CyCo
10-12-2004, 03:36 PM
OK, we can lose the iframe and do this (http://cyco.freeprohost.com/cgi-bin/demos/oc2.pl)
This method uses the html of the page to be either hardcoded or printed to the browser by opening header and footer files. Also, the Thank you page is a redirect (and the thank you page has a meta refresh that sends you back to the original page.) If you truly want to incorporate HTML and Perl for seamless production, you can check out HTML::Template or HTML::Mason.

Orsen Cart
10-16-2004, 09:23 AM
Hi CyCo

I've got most of my code working now but what I now really need to do is to get some the cgi generated HTMl to appear in 'in-line' within the HTML 'index' page. I can do that with the <iframe> you suggested in your first demo but I can't get it working without the iframe as per your the demo in your most recent post.

I guess I can't get it working because I don't understand your description of the method you used. From the code, it looks like you have all of the fields except the CITY field in the main HTML. The form is posted via the 'Show Cities For' button which runs oc2.pl . However, ever before the form is submitted, http://cyco.freeprohost.com/cgi-bin/demos/oc2.pl appears in the address line. When the form is submitted, the CITY field appears but the url doesn't change. I'm confused.

I also can't seem to see what is in your oc2.pl file; I only seem to be able to view the resulting HTML.

Is there any way that you can explain the method in a non-technical way (this is the first cgi script I've written) because your example seems to be exactly what I'm trying to achieve.

I also see that you used some .dat files to provide the data for the select lists which is also perfect for what I need to do. Would seeing your oc2.pl file help me to do that too?

Many thanks

CyCo
10-16-2004, 10:02 AM
This is oc2.pl

#!/usr/bin/perl

use strict;
use CGI qw/:standard *table/;

if (param('action') eq 'Submit') { &send; }
else { &start; }

sub start {
print header;

open(TOP, 'top.htm') || die $!;
print while(<TOP>);
close(TOP);

print start_form,start_table;

my @text_fields = qw/Name Email Address Zip/;
my $zip = pop(@text_fields);

for (@text_fields) { print Tr(td(["$_:",textfield($_),'&nbsp;'])); }

my $state = param('State');

if ($state) {
open(CITIES, "$state.dat") || die $!;
my @cities = <CITIES>;
close(CITIES) || die $!;
chomp @cities;
print Tr(td(['City:',popup_menu('City',\@cities),'&nbsp;']));
}

open(STATES, 'states.dat') || die $!;
my @states = <STATES>;
close(STATES) || die $!;
chomp @states;

print Tr(td(['State:',popup_menu('State',\@states),submit('','<- Show Cities For')])),
Tr(td(["$zip:",textfield($zip),'&nbsp;']));

print Tr(td(['&nbsp;',submit('action','Submit'),'&nbsp;'])) if $state;

print end_table,end_form;

open(BOT, 'bot.htm') || die $!;
print while(<BOT>);
close(BOT);
}

sub send {
print redirect('http://cyco.freeprohost.com/examples/thankyou.htm');
}


If you want to see every file involved let me know.

*NOTE* Everywhere you see a '&nbsp;&nbsp;' should be a non-breaking space. The forum turns it into '&nbsp;&nbsp;'

CyCo
10-16-2004, 10:09 AM
Just in case , here's a text version attached.