Hi,
I have written something in ModPerl (extremely proud of myself) but
have hit a snag. The program returns the input from a form.
Unfortunately I can't post a url to the form. The form has ten
sections, each section is a story synopsis. Within each story are 5
fields. I gather all the form data into a hash but once I have done
that I don't know how to find out how many stories I have.
I tried $num = %hash and this gave me "38/64" which I can't make
sense of.
$num = values(%hash) gives me the total number of fields (50) and
@num = values(%hash) seems to pour everything into the somewhere in
@num but I can't see to untangle it (I can't see anything in
$num[0]).
Can anyone help??
package ModPerl::mystory;
use Apache::Constants;
use Apache::Request;
use Date::Calc qw/Today/;
use strict;
use warnings;
use diagnostics;
use Template;
sub handler{
my $r = Apache::Request->new(shift);
my $date = &format_date;
my %hash = &parse_request($r);
my @stories = keys(%hash);
my $stories =0;
foreach my $d (@stories) {
my $v = $hash{"$d"};
# print STDERR "$hash{$d} => $v\n";
if (defined($v)) {
++$stories;
}
}
my $s = @stories;
my $test = "@stories, $stories[0], $stories[2] $stories[9] ";
if (defined($hash{a1_head}) ) {
my $vars = {
date => "$date",
hash => \%hash,
no_stories => "$stories",
test => "$test",
};
my $file = "inthenews.atm";
my $tt = Template->new({
INCLUDE_PATH => "/var/www/templates/",
OUTPUT => $r,
});
$r->content_type('text/html');
$r->send_http_header;
$tt->process($file,$vars)
|| die $tt->error;
}
else {
my $vars = {
date => "$date",
message => "not working",
};
my $file = "inthenews.atm";
my $tt = Template->new({
INCLUDE_PATH => "/var/www/templates/",
OUTPUT => $r,
});
$r->content_type('text/html');
$r->send_http_header;
$tt->process($file,$vars)
|| die $tt->error;
}
sub format_date {
my ($year,$month,$day) = Today();
my $date = "$day"."/"."$month"."/"."$year";
return $date;
}
sub parse_request {
my $r = shift;
my @no = (1...10);
my @flds = qw/head des spllink storyfrom storylink/;
my $l;
my %hash;
for ($l = 1;$l <=10;++$l) {
foreach my $p (@flds) {
my $param = "a"."$l"."_"."$p";
if (defined($r->param("$param")) ) {
# print STDERR "\$hash{$param} =>
".$r->param("$param")."\n";
$hash{$param} = $r->param("$param");
}
}
}
return %hash;
}
return OK;
}
1;
======== Form example =========
---------------------------------------------------------------------
Story 1
Headline <input field>
Description <input field>
SPL link <input field>
Story from <input field>
Story link <input field>
----------------------------------------------------------------------
Story 2
Headline <input field>
Description <input field>
SPL link <input field>
Story from <input field>
Story link <input field>
---------------------------------------------------------------------
====== Form source ======
<HTML>
<HEAD>
<TITLE></TITLE>
<META name="description" content="">
<META name="keywords" content="">
<META name="generator" content="CuteHTML">
</HEAD>
<style type="text/css">
p.top { font: bold 24/2 "fowndry" ;
margin-top: 10px }
p.note { font: 10pt "ariel"}
p.main { font: 16/1 "ariel";
line-height: 16pt }
p.ttitle { font: bold 12/2 "ariel" }
</style>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF"
VLINK="#800080">
<p class="top">
<b>"In the News" Form</b>
</p>
<p class="note">
All fields are limited to 300 characters. Up to 10 articles can be
added.
</p>
<hr size="2" width="100%"
<p class="main">
<b>Story 1</b>
<br>
<form action="http://dev.sciencephoto.co.uk/inthenews" method="get">
Headline <input type="text"
maxlength="300" size="120" name="a1_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a1_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a1_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a1_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a1_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 2</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a2_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a2_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a2_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a2_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a2_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 3</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a3_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a3_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a3_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a3_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a3_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 4</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a4_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a4_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a4_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a4_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a4_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 5</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a5_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a5_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a5_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a5_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a5_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 6</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a6_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a6_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a6_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a6_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a6_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 7</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a7_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a7_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a7_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a7_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a7_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 8</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a8_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a8_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a8_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a8_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a8_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 9</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a9_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a9_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a9_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a9_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a9_storylink" value="" align="left">
<hr size="2" width="100%">
<b>Story 10</b>
<br>
Headline <input type="text"
maxlength="300" size="120" name="a10_head" value="" align="left">
<br>
Description <input type="text" maxlength="300" size="120"
name="a10_des" value=""align="left">
<br>
SPL link <input type="text"
maxlength="300" size="120" name="a10_spllink" value=""align="left">
<br>
Story from  <input type="text" maxlength="300" size="120"
name="a10_storyfrom" value="" align="left">
<br>
Story link <input type="text" maxlength="300"
size="120" name="a10_storylink" value="" align="left">
<hr size="2" width="100%">
</p>
<input type="submit" value="Let's have it">
</form>
</BODY>
</HTML>
=======================================
~~
Dermot Paikkos * [EMAIL PROTECTED]
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>