P
Paul Lalli
Greetings. Using Perl and CGI.pm, can someone recommend a decent way
of
processing groups of inputs and parameters? As an example, say I have
a
form to record student grades:
<form method="post" action="grades.cgi">
John - <input type="radio" name="john_grade" value="A">
<input type="radio" name="john_grade" value="B">
<input type="radio" name="john_grade" value="C">
<input type="radio" name="john_grade" value="D">
<input type="radio" name="john_grade" value="F">
<input type="text" name="john_comments" value="">
<br />
Mary - <input type="radio" name="mary_grade" value="A">
<input type="radio" name="mary_grade" value="B">
<input type="radio" name="mary_grade" value="C">
<input type="radio" name="mary_grade" value="D">
<input type="radio" name="mary_grade" value="F">
<input type="text" name="mary_comments" value="">
<br />
<!-- etc -->
</form>
(This is for demonstration purposes - real code would use a unique
identifier, like student id, as the prefix).
As you can see, for large numbers of students, this would quickly
become
unweildy. I currently have only one thought as to how to process these
fields - retrieve a list of parameters, grep()'ing for the ones I want,
and
process the groups:
my @grades = grep { /_grade$/ } param();
for (@grades){
my $name = /^(\w+)_grade/;
print "$name has grade: ", param($_), "\n";
print "Comments: ", param($name.'_comments'), "\n\n";
}
That seems messy at best. This is especially annoying to me, because I
know
that in PHP, I can make my inputs look like this:
<input type="radio" name="grade[john]" value="A">
<input type="radio" name="grade[john]" value="B">
<input type="radio" name="grade[john]" value="C">
<input type="radio" name="grade[john]" value="D">
<input type="radio" name="grade[john]" value="F">
<input type="text" name="comments[john]" value="">
and then retrieve the list of names with:
$grades = $_POST['grades'];
foreach ($grades as $name => $grade) {
echo "$name has grade: $grade\n";
echo "Comments: $_POST[comments][$name]\n\n";
}
(syntax may not be correct - it's been a long while since I used
PHP...)
Is there any similar way I can work with CGI.pm's param() function to
retrieve a multi-dimensioned structure? Alternatively, can someone
suggest
a way of processing similarly-purposed fields like this? I strongly
doubt
that this is such a rare problem that someone hasn't already solved it.
I'm much more inclined to beileve that I'm either not thinking hard
enough
or not searching well enough.
Thanks for any assistance you can provide.
Paul Lalli
P.S. (I don't *think* this is a truly CGI problem, but if I would be
better off in the comp.web.authoring.cgi (or similar) group, please let
me know)
of
processing groups of inputs and parameters? As an example, say I have
a
form to record student grades:
<form method="post" action="grades.cgi">
John - <input type="radio" name="john_grade" value="A">
<input type="radio" name="john_grade" value="B">
<input type="radio" name="john_grade" value="C">
<input type="radio" name="john_grade" value="D">
<input type="radio" name="john_grade" value="F">
<input type="text" name="john_comments" value="">
<br />
Mary - <input type="radio" name="mary_grade" value="A">
<input type="radio" name="mary_grade" value="B">
<input type="radio" name="mary_grade" value="C">
<input type="radio" name="mary_grade" value="D">
<input type="radio" name="mary_grade" value="F">
<input type="text" name="mary_comments" value="">
<br />
<!-- etc -->
</form>
(This is for demonstration purposes - real code would use a unique
identifier, like student id, as the prefix).
As you can see, for large numbers of students, this would quickly
become
unweildy. I currently have only one thought as to how to process these
fields - retrieve a list of parameters, grep()'ing for the ones I want,
and
process the groups:
my @grades = grep { /_grade$/ } param();
for (@grades){
my $name = /^(\w+)_grade/;
print "$name has grade: ", param($_), "\n";
print "Comments: ", param($name.'_comments'), "\n\n";
}
That seems messy at best. This is especially annoying to me, because I
know
that in PHP, I can make my inputs look like this:
<input type="radio" name="grade[john]" value="A">
<input type="radio" name="grade[john]" value="B">
<input type="radio" name="grade[john]" value="C">
<input type="radio" name="grade[john]" value="D">
<input type="radio" name="grade[john]" value="F">
<input type="text" name="comments[john]" value="">
and then retrieve the list of names with:
$grades = $_POST['grades'];
foreach ($grades as $name => $grade) {
echo "$name has grade: $grade\n";
echo "Comments: $_POST[comments][$name]\n\n";
}
(syntax may not be correct - it's been a long while since I used
PHP...)
Is there any similar way I can work with CGI.pm's param() function to
retrieve a multi-dimensioned structure? Alternatively, can someone
suggest
a way of processing similarly-purposed fields like this? I strongly
doubt
that this is such a rare problem that someone hasn't already solved it.
I'm much more inclined to beileve that I'm either not thinking hard
enough
or not searching well enough.
Thanks for any assistance you can provide.
Paul Lalli
P.S. (I don't *think* this is a truly CGI problem, but if I would be
better off in the comp.web.authoring.cgi (or similar) group, please let
me know)