Page 1 of 1

js checkboxes, arrays & function calls

Posted: Thu Sep 15, 2005 10:13 am
by chatasos
We have repeated checkboxes using one of the following formats

format 1

Code: Select all

<input type="checkbox" name="mods_check[]" />
format 2

Code: Select all

<input type="checkbox" name="mods_check" />
After user submission:

In the 1st case, $HTTP_POST_VARS['mods_check'] is an array.
In the 2nd case, $HTTP_POST_VARS['mods_check'] isn't an array.

BUT....

In the 1st case a js function call like

Code: Select all

check_all(document.form.mods_check)
....
function check_all(field)
{
	alert(field.length);
}
isn't working (field is undefined), while in the 2nd it's working fine.

Is there a way i can have the 1st format working in the js function?

Posted: Thu Sep 15, 2005 10:38 am
by chatasos
To answer my own question (once more :lol: ), the following seems to do the job

Code: Select all

check_all(document.form.elements['mods_check[]']);

Posted: Thu Sep 15, 2005 10:44 am
by afterlife_69
HTML doesnt have arrays, thats why it didnt work before.

Posted: Thu Sep 22, 2005 10:15 pm
by chatasos
Well, it seems i have another problem here:

This is the javascript function:

Code: Select all

function check_all(field)
{
	alert(field.length);
}
This is the html code with 1 checkbox

Code: Select all

<form name="mod" method="post" action="{S_MOD_ACTION}">

<input type="checkbox" name="mods_check[]" value="3"  />

<input type="checkbox" name="global_check" onClick="check_all(document.mod.elements['mods_check[]'])" />
This is the same html code with 2 checkboxes

Code: Select all

<form name="mod" method="post" action="{S_MOD_ACTION}">

<input type="checkbox" name="mods_check[]" value="3"  />
<input type="checkbox" name="mods_check[]" value="4"  />

<input type="checkbox" name="global_check" onClick="check_all(document.mod.elements['mods_check[]'])" />
PHP is working fine on both cases and i get the 1 or 2 values accordingly to my checkbox selections.

But the javasciript function is returning "undefined" in the first case and "2" in the second. It's like it doesn't recognise the checkbox element when there 's only one and it's called using [].

Posted: Thu Sep 22, 2005 11:12 pm
by chatasos
I found the solution by parsing all input elemement of the form and counting only the ones that have type='checkbox' and name="mods_check[]".

:D :D :D