I'
m
trying
to
use
an
input
type="image
"
tag
,
but
the
$foo.x
and
$foo.y
variables
aren
't
available
.
When the user clicks somewhere on the image, the
accompanying form will be transmitted to the server with
two additional variables: foo.x and foo.y.
Because
$foo.x
and
$foo.y
are
invalid
variable
names
in
PHP
,
they
are
automagically
converted
to
$foo_x
and
$foo_y
.
Notice the square brackets after the variable name, that's
what makes it an array. You can group the elements into
different arrays by assigning the same name to different
elements:
This produces two arrays, MyArray and MyOtherArray, that
gets sent to the PHP script. It's also possible to assign
specific keys to your arrays:
The AnotherArray array will now contain the keys 0, 1,
email and phone.
Each selected option will arrive at the action handler as:
Each option will overwrite the contents of the previous
$var
variable. The solution is to use PHP's "array from form
element" feature. The following should be used:
This tells PHP to treat
$var
as an array and each assignment of a value to var[] adds an
item to the array. The first item becomes
$var[0]
, the next
$var[1]
, etc. The
count()
function can be used to determine how many options were
selected, and the
sort()
function can be used to sort the option array if necessary.