Checkbox into an Array ?

S

Sylvie Stone

Hi group - I have an html form where users can query the database
based on a bunch of criteria but I'm having trouble capturing all the
checked checkboxes to build the select statement. In php I would just
use implode.

I've been trying to use 'join' but to no avail.
The checkbox options are generated dynamically:

$dbh->prepare("select id,name from statuslookup where inactive is NULL
order by id");
$sth->execute();
my $ref_allrows = $sth->fetchall_arrayref;
my $i;
for $i (0 .. $#{$ref_allrows}) {
print "<input type=checkbox name=\"statuscodearray[]\"
value=\"$ref_allrows->[$i][0]\">$ref_allrows->[$i][0]
$ref_allrows->[$i][1]";
}

Then to process the form onsubmit:

<snip>

my @statuscodearray;
my $statuscode = join(" ", @statuscodearray);

<snip>

if (length($statuscode) > 0) {
$build_query = "$build_query and statuslink = '$statuscode'";
}


The array will hold integers like this:
select blah from blahblah where statuslink in ('1', '44', '55', '77')
..... ;

THANK YOU ! This is driving me crazy!

Sylvie.
 
C

ctcgag

Hi group - I have an html form where users can query the database
based on a bunch of criteria but I'm having trouble capturing all the
checked checkboxes to build the select statement. In php I would just
use implode.

I've been trying to use 'join' but to no avail.
The checkbox options are generated dynamically:

$dbh->prepare("select id,name from statuslookup where inactive is NULL
order by id");
$sth->execute();
my $ref_allrows = $sth->fetchall_arrayref;
my $i;
for $i (0 .. $#{$ref_allrows}) {
print "<input type=checkbox name=\"statuscodearray[]\"
value=\"$ref_allrows->[$i][0]\">$ref_allrows->[$i][0]
$ref_allrows->[$i][1]";
}

Is it legal to have [] in the name of a form element?

Then to process the form onsubmit:

Yes, you need to process the form on submit!

<snip>

my @statuscodearray;
my $statuscode = join(" ", @statuscodearray);

Shouldn't you try to fill @statuscodearray with the form
results before you use it? Something like
@statuscodearray=$q->params('statuscodearray[]');
<snip>

if (length($statuscode) > 0) {
$build_query = "$build_query and statuslink = '$statuscode'";
}

The array will hold integers like this:
select blah from blahblah where statuslink in ('1', '44', '55', '77')
.... ;

THANK YOU ! This is driving me crazy!

$build_query.= "and statuslink in (" .
join (',', map "'$_'", @statuscodearray) . ")";


Xho
 
D

David K. Wall

The checkbox options are generated dynamically:

$dbh->prepare("select id,name from statuslookup where inactive is
NULL order by id");
$sth->execute();
my $ref_allrows = $sth->fetchall_arrayref;
my $i;
for $i (0 .. $#{$ref_allrows}) {
print "<input type=checkbox name=\"statuscodearray[]\"
value=\"$ref_allrows->[$i][0]\">$ref_allrows->[$i][0]
$ref_allrows->[$i][1]";
}

Is it legal to have [] in the name of a form element?

No. See http://www.w3.org/TR/html4/types.html#type-cdata -- which of
course has nothing to do with Perl.

In any case, the loop could be written more Perlishly:

[untested code]

for my $row (@$ref_allrows) {
print qq{<input type=checkbox
name="statuscode"
value="$row->[0]">$row->[0]$row->[1]};
}

Using some of the CGI.pm shortcuts might be better, depending on
taste. <shrug>
 
T

Tad McClellan

Sylvie Stone said:


$i is now a global variable, it does not need to be global since
you use it only inside of the loop.

for $i (0 .. $#{$ref_allrows}) {
print "<input type=checkbox name=\"statuscodearray[]\"
value=\"$ref_allrows->[$i][0]\">$ref_allrows->[$i][0]
$ref_allrows->[$i][1]";
}


You could declare $i in the foreach statement.

for my $i (0 .. $#{$ref_allrows}) {

You do not need explicit indexing at all, so you don't even need $i.

You can use alternate delimiters to avoid the backslashing.

# untested
foreach my $row ( @$ref_allrows ) {
print '<input type=checkbox name="statuscodearray[]"',
qq(value="$row->[0]">$row->[0]$row->[1]);
}

my @statuscodearray;
my $statuscode = join(" ", @statuscodearray);


@statuscodearray *must* contain the empty list at this point...

if (length($statuscode) > 0) {


.... so that condition will never be true.

Don't you want to put something into the array before join()ing it?
 
A

Andy Hassall

Is it legal to have [] in the name of a form element?

No. See http://www.w3.org/TR/html4/types.html#type-cdata

Do you have a reference in the specification where it limits the valid
characters for the name attribute of the input element?

http://www.w3.org/TR/html4/interact/forms.html#h-17.4

... shows name to be of type CDATA; not of the restricted types ID or NAME.

http://www.w3.org/TR/html4/interact/forms.html#control-name

... doesn't appear to have any notes on restrictions not represented by the
DTD. It says to refer to the individual control for any further restrictions;
the notes for INPUT doesn't list any.

http://www.w3.org/TR/html4/interact/forms.html#adef-name-INPUT

Would be interesting to find something in the specification that explicitly
says that [] is illegal in the name, since PHP uses that for producing arrays
out of sets of checkboxes as in the OP's form, for example.
which of course has nothing to do with Perl.

Hm, yes, probably need to take it to comp.infosystems.www.authoring.html or
similar.
 
T

Tad McClellan

David K. Wall said:
(e-mail address removed) (Sylvie Stone) wrote:
print "<input type=checkbox name=\"statuscodearray[]\"
Is it legal to have [] in the name of a form element?

No. See http://www.w3.org/TR/html4/types.html#type-cdata


That is a _different_ NAME.

A "NAME" in SGML is what a programmer might be used to
calling an "identifier".

In:

<input name="statuscodearray[]">&nbsp;</input>
^^^^^ ^^^^ ^^^^ ^^^^^

There are 4 NAMEs. They must each follow the rules at the URL you cite above.

"input" is the NAME of an element, so it is used in the start tag.

And the end tag.

"name" is the NAME of an attribute on the input element.

"nbsp" is the NAME of a general entity.


The "name" in the "Is it legal" part above is yet a third
overloaded use of the term.

_That_ name is actually a _value_ (of an attribute) as far
as the markup language is concerned.


You cannot tell what is allowed for the value of the name attribute
without access to the DTD being used.
 
D

David K. Wall

[rather than follow up two posts, I'll follow up myself and summarize (sort
of)]
Is it legal to have [] in the name of a form element?

No. See http://www.w3.org/TR/html4/types.html#type-cdata

Andy Hassell and Tad McClellan have both demonstrated that I was wrong.
Consider the statement retracted, and, as always, thanks for the correction.

I still think I'll avoid such usage myself, unless I decided to use PHP and
absolutely have to. :)
 

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

Forum statistics

Threads
474,121
Messages
2,570,714
Members
47,282
Latest member
hopkins1988

Latest Threads

Top