Hi, All
I have a form which contains an array checkboxes. Examples:
<form method ="post" name="form1" id="form1">
<input type="checkbox" name="object[]" id="object[]" value="1">
<input type="checkbox" name="object[]" id="object[]" value="2">
</form>
I using PHP get value of array checkboxes. However, I want to check data of array checkboxes
by javascript. Example:
<script language="jscript">
function CheckData() {
var Form1 = document.forms['form1'].object
var ok = 0
for (i=0;i<Form1.object.length;i++)
{
if (Form1.object.checked) ok = 1
return true
}
if ( ok==0) {
alert("Data invalid")
return false
}
}
</script>
Is it the fact that Javascript desn't understand the [] at the end of the name's checkbox.
Javascript errors "object.length is null or not an object". Unfortunately I need the [] there
as they are to be processed by some php when I submit the form.
Thanks for all your help
help use the array checkbox in javascript
Moderator: General Moderators
-
zelot1980_vn
- Forum Newbie
- Posts: 2
- Joined: Thu Jul 03, 2003 3:07 am
- Location: programmer
- Contact:
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
If you leave the [] out of the form-field ID, you'll be fine (leave them in the name, though).
Just another thing - I am not sure whether your form-field name "object" isn't a reserved keyword. I would give it a more descriptive name such as "checkBoxGender".
Checking if checkboxes have a value:
I hope now Bob's your uncle again 
Code: Select all
<form method ="post" name="form1" id="form1">
<input type="checkbox" name="objectї]" id="object" value="1">
<input type="checkbox" name="objectї]" id="object" value="2">
</form>Checking if checkboxes have a value:
Code: Select all
function checkBoxValue(item)
{
for(i=0;i<document.getElementById(""+item+"").length;i++)
if(document.getElementById(""+item+"")їi].checked)
return document.getElementById(""+item+"")їi].value;
}