batfastad
08-10-2005, 03: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
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