Mailing List Archive



Back to the month index Back to the list index

Jason Armstrong (jason@datrix.co.za)
Mon, 10 Nov 1997 20:56:42 +0200 (SAT)


Date: Mon, 10 Nov 1997 20:56:42 +0200 (SAT)
From: Jason Armstrong <jason@datrix.co.za>
Subject: Re: FORM with checkboxes
Message-ID: <Pine.LNX.3.95.971110205335.8045A-100000@shark.datrix.co.za>

There is also the typeof operator, which lets you know what kind of
variable you are dealing with, array or scalar. You say:

$type = typeof($var);
if ($type == "char") {
        if ($var != "") {
        /* scalar variable */
        }
}
if ($type == "array") {
        /* array */
}

On Fri, 7 Nov 1997, Ernesto Baschny wrote:

> Hello!
>
> I just had an idea to solve a problem that I had which I just want to
> share, maybe it helps someone:
>
> If you have checkboxes (or multiple selectable items) in a FORM,
> and send the results to a w3-msql script, you will end up with:
>
> 1) a single variable, if only one checkbox was marked
> 2) an array, if there were two or more marked items
>
> This leads to the problem where you cannot identify if you have the one
> or the other (or is there any way?). If you try to access it as an array
> and it is a single variable, you get an error, if you try to access a
> single variable with something like $var[0], you will get "(NULL)". The
> "#" operator also behaves differently with both types, so this cannot be
> used to differentiate them.
>
> So what I did is to guarantee that it is always an array, by having
> TWO hidden bogus entries at the beginning of the form, so that it will
> always be an array. Example:
>
> <FORM METHOD=POST ACTION="test.msql">
> <INPUT TYPE=hidden NAME=add VALUE=bogus>
> <INPUT TYPE=hidden NAME=add VALUE=bogus>
> <INPUT TYPE=checkbox NAME=add VALUE=10> 10<BR>
> <INPUT TYPE=checkbox NAME=add VALUE=11> 11<BR>
> <INPUT TYPE=checkbox NAME=add VALUE=12> 12<BR>
> <INPUT TYPE=submit VALUE="Submit">
> </FORM>
>
> This will *always* create the "add" array, where the two first entries
> are "bogus". Beginning with $add[2] are the actual values. $add[2] will
> be "(NULL)" if none were checked.
>
> I can't see any implication with this, maybe there is a better sollution,
> if there is, I would be glad to hear about it.
>
>
>
> --
> Ernesto <baschneo@trick.informatik.uni-stuttgart.de>
> PEM GmbH <ernst@pem.com>
> Stuttgart - Germany
>

        Jason Armstrong
        jason@datrix.co.za