note 23174 deleted from language.variables.predefined by dams
| From: | dams@php.net | Date: | Sun, 28 Jul 2002 14:48:18 +0000 |
| Subject: | note 23174 deleted from language.variables.predefined by dams | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-33817@lists.php.net to get a copy of this message | ||
Like others, I was struggling with getting arrays of checkboxes into a PHP array.
The notes here have helped, but simply changing the name (vimal@uta.edu 29-Apr-2002) to
'something[]' produces syntax errors when trying do something (anything!) useful with the
controls in JavaScript - for example, using a JS function to select or de-select each control in the
array - so I came up with this...
Have pairs of controls:
<INPUT TYPE="checkbox" NAME="CB">
<INPUT TYPE="hidden" NAME "CB_VALUE" VALUE="whatever">
Replace the submit button with:
<INPUT TYPE="button" onClick="doSubmit();">
This JS function changes the checkbox name from CB_VALUE to CB_VALUE[], submits the form and
presents PHP with CB_VALUE[0] to CB_VALUE[n].
function doSubmit() {
for (i=1;i<document.forms[0].CB.length;i++) {
document.forms[0].CB_VALUE[i].name='CB_VALUE[]';
}
document.forms[0].submit();
}
To only present checked items in the resulting PHP array amend the function to:
if (document.forms[0].CB[i].checked==true) {
document.forms[0].CB_VALUE[i].name='CB_VALUE[]';
}
Much to my surprise it actually works.
Andrew (PHP novice).