Click to See Complete Forum and Search --> : passing a checkbox array on a form to a function


mike@spb
August 29th, 2002, 04:32 PM
I have a form with 6 check boxes:

<td><input type=checkbox Name=Weekday[]>Sunday</td>
<td><input type=checkbox Name=Weekday[]>Monday</td>
<td><input type=checkbox Name=Weekday[]>Tuesday</td>
<td><input type=checkbox Name=Weekday[]>Wednesday</td>
<td><input type=checkbox Name=Weekday[]>Thursday</td>
<td><input type=checkbox Name=Weekday[]>Friday</td>
<td><input type=checkbox Name=Weekday[]>Saturday</td>

[/code;]
I want to be able to varify that at least one check box was checked.

Here's what I want to do:

[code]

function CheckWeekday(form){
var Weekday = new Array(7);
var retval;

for(i=0;i<7;i++){
Weekday[i] = Schedule.Weekday[i].value;
if(Weekday[i] != ""){
retval=true; //Checkbox has been checked
return retval;
}
}
}


I get an error that Schedule.Weekday.0 is null or not an object. What am I doing wrong?

Mike@spb

}

anupam kant
August 30th, 2002, 12:38 AM
1. remove the square bracket with all the names like:

<td><input type=checkbox Name=Weekday>Sunday</td>
............

2. I want to change the function to:

function CheckWeekday(){
var retval=-1;
for(i=0;i<7;i++){
if(Schedule.Weekday[i].checked)
{
return true;
break;
}
}
return false;
}

this will return true if anyone of the checkbox is checked.

the reason error occured is you used [] with the names.

hope this helps

mike@spb
August 30th, 2002, 02:56 PM
I'm also using php and I need those "[]" for the array that is passed. I used "elements[i]' of the fomr to find out if the checkboxes are checked.

anupam kant
September 1st, 2002, 11:41 PM
Hello Mike,
I fear I don't know much about php, but I beleive that the code should work well with php also. See the point is if you generate more than one html element with the same name/id, an array of elements automatically created and that can be accessed by the same name as the element name. (on client side) thats' why you don't need to use [] with the element name, again the elements can be accessed using [].

try this...:)