You can iterate trough forum controls with Javascript
var i;
for(i=1;i<18;i++) {
document.formPermissions.elements[i].checked = 1;
}
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
What you need to do in PHP is give each checkbox an individual ID then,
as in:
echo "<input type='checkbox' id='checkbox".$i."' name='".$checkboxName."'> </input>";
Now you can iterate trough them in javascript by doing this:
var i;
var isItChecked;
for(i=0;i<9;i++) {
isItChecked = document.myFormName.elements[i].checked;
}
Hope this helps ^^
brecht
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
I should stop writing my posts so late, anyway
Looking back at my previous post I don't even think you HAVE to give them a unique ID to be able to iterate trough them. ID is there to give each element a unique identifier though, I cannot locate that site you talk of, maybe you can point me to it so I can read up on that. Anyway, if you could give me a piece of code I could try and help with that, see where that goes.
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
onclick="if(this.checked == 1) { alert('checked: '+<? echo $newArray['id'];?>); }"
Hopefully this is what you want : - )
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
function checkCheckBoxes() {
var i;
var isOneChecked = false;
for(i=0;i<5;i++) {
if(checkBoxCountry[i].checked) {
isOneChecked = true;
}
}
return isOneChecked;
}
<input type="submit" onclick="alert(checkCheckBoxes())" value="Submit">
This should work. If you want to make it dynamic you can do that with PHP.
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
Excuse me, my previous post was indeed nonsense. This on the other hand should work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
function checkCheckBoxes(formName) {
var i;
var numberOfChecked = 0;
for(i=0;i<5;i++) {
if(formName.elements['checkBoxCountry['+i+']'].checked) {
numberOfChecked++;
}
}
return numberOfChecked;
}
</script>
</head>
<body>
<form name="myForm">
<input type="checkbox" name="checkBoxCountry[0]"></input>
<input type="checkbox" name="checkBoxCountry[1]"></input>
<input type="checkbox" name="checkBoxCountry[2]"></input>
<input type="checkbox" name="checkBoxCountry[3]"></input>
<input type="checkbox" name="checkBoxCountry[4]"></input>
<input type="submit" onclick="alert('Number of checked checkBoxes: ' + checkCheckBoxes(myForm))" value="Submit">
</form>
</body>
</html>
Good luck
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
That depends on what's in $new['studentId']. Is it a number? I'm guessing it is, and how many do you have, in my example there are 5 checkboxes in the form. If your studentId's follow each other up like this:
1, 2, 3
and not like:
3, 34, 55, 56, 57
You can probably change this Javascript:
function checkCheckBoxes(formName) {
var i;
var numberOfChecked = 0;
for(i=0;i<5;i++) {
if(formName.elements['checkBoxCountry['+i+']'].checked) {
numberOfChecked++;
}
}
return numberOfChecked;
}
to this:
function checkCheckBoxes(formName) {
var i = 0;
var numberOfChecked = 0;
while(formName.elements['checkBoxCountry['+i+']']) {
if(formName.elements['checkBoxCountry['+i+']'].checked) {
numberOfChecked++;
}
i++;
}
return numberOfChecked;
}
EDIT: code tested and should work
Hope this helps you ^^
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
What you could do is give each checkbox an ID and go by that ID instead of the name, then you can get the name of that checkbox because you have it's ID:
<input type="checkbox" id="checkBox1" name="checkBoxCountry[01SW]"></input>
<input type="checkbox" id="checkBox2" name="checkBoxCountry[A22]"></input>
<input type="checkbox" id="checkBox3" name="checkBoxCountry[BX2L]"></input>
<input type="checkbox" id="checkBox4" name="checkBoxCountry[11OX]"></input>
<input type="checkbox" id="checkBox5" name="checkBoxCountry[41W]"></input>
If you have any questions just reply, I don't have much time to react now I'm sorry
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9
Great to hear that! :)
Good luck on learning Javascript
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9