hi guys,
I am using a php script where I am using an array of checkboxes. Now, i was trying to use a javascript function that will stop the user to go to next page if they dont check any checkboxes.
Here goes my php code:
echo ‘<form name=”form” method=”post” action=”updateLink.php”
onSubmit=”return validateForm(form);”>;
while($row=$connector->fetchArray($result)) {
echo ‘<p>
<input type=”checkbox” name=”C1[]” value=’.$row[‘link_id’].’>
<input name=”T’.$row[‘link_id’].'” type=”text” size=”20″ value= “‘.$row[‘link’].'”>
</p>
<input type=”hidden” name=”section” value=’.$section.’>
<br />
‘;
}
echo ‘<center> <input type=”submit” name=”update” value=”Update”> </form></center>’;
and here is my javascript funciton:
function validateForm(form)
{
// set var check_choice to false
var checkbox_choice = false;
// Loop from zero to the one minus the number of check boxes
for(i=0;i<form.C1.length;i++)
// If there was a selection made
if(form.C1.checked){
checkbox_choice = true;
}
// If there were no selections made display an alert box
if (!checkbox_choice)
{
alert(“Please check atleast one link.”)
return (false);
}
}
I am completely new to javascript, so i probably missing something with presenting the array thing properly in my function. But to get the length of an array you use C1.lentgh and not C1[].length, right?
What am i doing wrong?