...

View Full Version : form input name containing []



batfastad
08-10-2005, 04:20 PM
Ok I've got an HTML form (name=xrefselector), to post to a PHP script

It consists of about 300 checkboxes and various other form controls.
All the checkboxes have the same name="" attribute taginput[], with the value="" attribute varying between them.

I have to have them called taginput[] with square brackets because when the form data gets sent to my PHP script, the checkbox data is already in an array which is exactly what I need.

Why's this in the JavaScript forum?

Well I've got the following javascript function...



function checkCount(form) {
var total = 0;
var max = form.taginput.length;

for (var idx = 0; idx < max; idx++) {
if (eval("document.xrefselector.taginput[" + idx + "].checked") == true) {
total += 1;
}
}

document.xrefselector.checkboxcounter.value = total + " selected";
}


And



onClick="checkCount(this.form)"

An onClick event handler on each of the checkboxes

I have a readonly text field (name=checkboxcounter) which acts as a counter, counting how many checkboxes are selected, which is updated after every click on a checkbox.

This is the problem...
The script works fine if all the checkboxes are named taginput, but I need to name them taginput[]

Though calling them taginput[] then gives me JavaScript errors - when I replace taginput in my JavaScript code with taginput[]

So on two lines in particular...


var max = form.taginput.length;
...becomes...
var max = form.taginput[].length;

...and...

if (eval("document.xrefselector.taginput[" + idx + "].checked") == true) {

...becomes...
if (eval("document.xrefselector.taginput[][" + idx + "].checked") == true) {


Which give me JavaScript errors

Can anyone see a way round this?
The name of the checkboxes needs to be taginput[] with square brackets, but is there anyway I can escape those in my JavaScript, but still have the JavaScript working?


Any ideas?

Thanks

Ben

martin_narg
08-10-2005, 04:48 PM
This should work for you mate:



function checkCount(f) {
var counter = 0;
for(var i=0; f.elements[i]; i++) {
if(f.elements[i].tagName.toLowerCase() == "input" && f.elements[i].type == "checkbox" && f.elements[i].checked) {
counter++;
}
}

document.xrefselector.checkboxcounter.value = counter + " selected";
}


However I should point out that using non alphanumeric characters (apart from underscores) in form element names can be hazardous and presents all sorts of problems. There's all sorts of ways around it - often best to replace characters that could potentially cause a problem with something less volatile (eg all whitespaces with underscores, removing square brackets, etc) or simply removing them altogether.

php != html != javascript

Hope this helps

m_n

batfastad
08-10-2005, 05:02 PM
That is awesome!!!

Works straight off.


Yeah I know it's not recommended to do that, but it's a really convenient way to get checkbox data into its own array for processing by PHP.

In this case these pages are just being used on a local intranet.

Thanks for the awesome help mate!!!


Ben

Kor
08-10-2005, 06:09 PM
However I should point out that using non alphanumeric characters (apart from underscores) in form element names can be hazardous and presents all sorts of problems.

Only when it's about the first character. And, of course, should be avoid the usual submit separators (& | % ... aso.) which might counfuse the CGI code. For the , let's say, "neutral" special characters (such as underscore), the safest way is to use the full (square brackets) notation;

document.forms['form_name'].elements['element_name']

martin_narg
08-10-2005, 06:24 PM
Only when it's about the first character. And, of course, should be avoid the usual submit separators (& | % ... aso.) which might counfuse the CGI code. For the , let's say, "neutral" special characters (such as underscore), the safest way is to use the full (square brackets) notation;

document.forms['form_name'].elements['element_name']

Yeah mate, I know what you mean and agree, however with any reserved characters in form/variable/object/etc names then it does make work slightly trickier for subsequent coders to easily read. It works, and the same rules and logic apply, but for cleanliness sake I have always found it is better to steer clear of them.

I guess it is "each to their own", but you rarely see unusual characters in form/variable/object/etc names in top notch code. It's a personal preference really of mine.

Ultimately, I guess, the code works (thank you for the compliment batfastad) and that's what's really important =)

m_n



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum