js checkboxes, arrays & function calls

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
chatasos
Registered User
Posts: 748
Joined: Wed May 15, 2002 1:16 pm
Location: Paralia

js checkboxes, arrays & function calls

Post 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?

Report Posts 1.2.3c (MODDB) - Report Posts 2.1.5 (ALPHA)
Rebuild Search 2.4.0 (MODDB)
MOD Version Checker 1.2.0 (MODDB)
Mega Mail System 0.9.8 (ALPHA)
Pagination Select List & Input Box (MODDB)

chatasos
Registered User
Posts: 748
Joined: Wed May 15, 2002 1:16 pm
Location: Paralia

Post 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[]']);

Report Posts 1.2.3c (MODDB) - Report Posts 2.1.5 (ALPHA)
Rebuild Search 2.4.0 (MODDB)
MOD Version Checker 1.2.0 (MODDB)
Mega Mail System 0.9.8 (ALPHA)
Pagination Select List & Input Box (MODDB)

afterlife_69
I've Been Banned!
Posts: 630
Joined: Tue Nov 30, 2004 10:35 am

Post by afterlife_69 »

HTML doesnt have arrays, thats why it didnt work before.

chatasos
Registered User
Posts: 748
Joined: Wed May 15, 2002 1:16 pm
Location: Paralia

Post 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 [].

Report Posts 1.2.3c (MODDB) - Report Posts 2.1.5 (ALPHA)
Rebuild Search 2.4.0 (MODDB)
MOD Version Checker 1.2.0 (MODDB)
Mega Mail System 0.9.8 (ALPHA)
Pagination Select List & Input Box (MODDB)

chatasos
Registered User
Posts: 748
Joined: Wed May 15, 2002 1:16 pm
Location: Paralia

Post 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

Report Posts 1.2.3c (MODDB) - Report Posts 2.1.5 (ALPHA)
Rebuild Search 2.4.0 (MODDB)
MOD Version Checker 1.2.0 (MODDB)
Mega Mail System 0.9.8 (ALPHA)
Pagination Select List & Input Box (MODDB)

Post Reply

Return to “[2.0.x] MOD Writers Discussion”

cron