RE: [PHP] dealing with arrays
| From: | Ford, Mike [LSS] | Date: | Wed, 27 Aug 2003 14:38:00 +0000 |
| Subject: | RE: [PHP] dealing with arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-160916@lists.php.net to get a copy of this message | ||
On 27 August 2003 04:22, Aris Santillan wrote:
> I Have
> <p>Entry One
> <input type=text name=name[]>
> <input type=text name=tel[]>
> <input type=checkbox name=check[] value=1>
>
> <p>Entry Two
> <input type=text name=name[]>
> <input type=text name=tel[]>
> <input type=checkbox name=check[] value=1>
>
> <p>Entry Three
> <input type=text name=name[]>
> <input type=text name=tel[]>
> <input type=checkbox name=check[] value=1>
>
> <p>Entry Four
> <input type=text name=name[]>
> <input type=text name=tel[]>
> <input type=checkbox name=check[] value=1>
>
> <p>Entry Five
> <input type=text name=name[]>
> <input type=text name=tel[]>
> <input type=checkbox name=check[] value=1>
>
> .
>
> i want to process entries only with checked checkbox
>
> how can i do this in php?
This happens anyway, as only the values for checked checkboxes are returned to the script --
you'll never get to see unchecked boxes. However, the way you've written the form causes
a problem here: since all text boxes, but only checked checkboxes, are sent, you'll see 5
elements in the name[] and tel[] arrays (name[0] to name[4] and tel[0] to tel[4]) but only as many
entries in the check[] array as there are boxes checked -- you might get, for example, check[0] and
check[1], so you know 2 boxes were checked but not *which* boxes (it might have been the ones for
Entry Three and Entry Five).
The way to combat this is to use explicit subscripts in your form field names, like so:
<p>Entry One
<input type=text name=name[1]>
<input type=text name=tel[1]>
<input type=checkbox name=check[1] value=1>
<p>Entry Two
<input type=text name=name[2]>
<input type=text name=tel[2]>
<input type=checkbox name=check[2] value=1>
<p>Entry Three
<input type=text name=name[3]>
<input type=text name=tel[3]>
<input type=checkbox name=check[3] value=1>
<p>Entry Four
<input type=text name=name[4]>
<input type=text name=tel[4]>
<input type=checkbox name=check[4] value=1>
<p>Entry Five
<input type=text name=name[5]>
<input type=text name=tel[5]>
<input type=checkbox name=check[5] value=1>
Now, you'll still get all five entries for name[] and tel[] ([1] to [5] in this instance), but
you'll also helpfully get the corresponding elements of check[] for the boxes which were
actually checked -- so, if only Entry Two and Entry Four are checked, you'll get check[2] and
check[4] set (but no other elements of the check[] array). All you need to do is foreach() along
the check array, and take action for each key that that returns you.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211