Why can't I access an Array like this?

A

A. Sinan Unur

SNAME0001, SPASS0001, FRONT0001, BACK0001, TITLE0001, COPY0001,
TOC0001, FOR0001, ACK0001, CHAP0001 and the person using the form can
add / remove groups of fields (these 10 fields all ending with the
same numbers). I add and remove the fields dynamically in the perl
script to the web page, so I only know the starting letters of the
field (ie SNAME, SPASS, FRONT etc) and how many groups are on the
page. So I have to work my way through the groups of fields using a
for loop (hence the need for the 0001 - ????) to build the page.

No, there is no need for that. The following script is *very* quick and
dirty but it might be useful:

#!perl

use strict;
use warnings;

use CGI;
use Data::Dumper;
use HTML::Entities;
use HTML::Template;

my $tmpl = <<EO_TMPL;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Field Adder Test</title>
</head>
<body>
<h1>Test Form</h1>
<form method="POST">
<TMPL_LOOP FIELDS>
<p><input name="myfield" value="<TMPL_VAR MYFIELD_VALUE>"></p>
</TMPL_LOOP>
<p><input type="submit" name="action" value="Submit">
<input type="submit" name="action" value="Add Field"></p>
</form>
</body>
</html>
EO_TMPL

my $cgi = CGI->new;

my $action = $cgi->param('action');

if ( $action eq 'Add Field' ) {
handle_add_field();
}
elsif ( $action eq 'Submit' ) {
handle_dump_form();
}
else {
handle_show_form();
}


sub handle_show_form {
my $template = HTML::Template->new( scalarref => \$tmpl );
$template->param( FIELDS => [ { MYFIELD_VALUE => q{} } ] );

print $cgi->header('text/html'), $template->output;
}

sub handle_dump_form {
print $cgi->header('text/plain'), Dumper( $cgi );
}

sub handle_add_field {
my @fields_loop;

push @fields_loop, map {
{ MYFIELD_VALUE => encode_entities( $_ ) }
} $cgi->param('myfield');

push @fields_loop, { MYFIELD_VALUE => q{} };

my $template = HTML::Template->new( scalarref => \$tmpl );
$template->param( FIELDS => \@fields_loop );

print $cgi->header('text/html'), $template->output;
}

__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top