I have a form that I validate using JavaScript.
Now when I submit the form, I cannot use the checkboxes in this form.
This is the part of my JS that counts and checks the checkboxes.
<script type="text/javascript">
function validateForm()
{
var cases = addClients.caseType.length;
var total = 0;
var fnReturn = true;
for (var idx = 0; idx < cases; idx++)
{
if (addClients.caseType[idx].checked == true)
total += 1;
}
if (total == 0)
{
document.getElementById('caseErr').style.visibility =" visible";
fnReturn = false;
}
else
document.getElementById('caseErr').style.visibility = "hidden";
return (fnReturn == true)?true:false;
}
</script>
The next page to which the form submits data is as follows (part of the flags):
<?php
$cases = $_POST['caseType'];
for ($i=0;$i<count($cases);$i++)
echo "<li>$cases[$i] \n";
When pressed again, $casesonly one letter of the selected flag is displayed.
I want to know what I'm doing wrong here.
EDIT:
This is my form
<form id="addClients" method="post" action="confirmAdd.php" onsubmit="return validateForm();">
<table border="0">
<tr align="center">
<th align="center">Client Last Name:</th>
<td colspan="3" align="center"><input type="text" name="clname" width="300%"></td>
<td id="lnErr" style="visibility:hidden">Last Name Required!</td>
</tr>
<tr align="center">
<th align="center">Client First Name:</th>
<td colspan="3" align="center"><input type="text" name="cfname" width="300%"></td>
<td id="fnErr" style="visibility:hidden">First Name Required!</td>
</tr>
<tr align="center">
<th align="center">Client Telephone:</th>
<td colspan="3" align="center"><input type="text" name="ctel" width="300%"></td>
<td id="telErr" style="visibility:hidden">Telephone Required (without hyphins '-')!</td>
</tr>
<tr align="center">
<th>Cases:</th>
<td align="center">Rape<input type="checkbox" name="caseType[]" value="rape"></td>
<td align="center">Drug Accusition<input type="checkbox" name="caseType[]" value="Drug Accusition"></td>
<td align="center">Assult<input type="checkbox" name="caseType[]" value="Assult"></td>
<td id="caseErr" style="visibility:hidden">At least one required!</td>
</tr>
</table>
<input type="submit" value="Add Client">
</form>
DECISION:
I managed to find a solution to the problem using googling ...
first the name of each flag has []
Rape<input type="checkbox" name="caseType[]" value="rape">
Drug Accusition<input type="checkbox" name="caseType[]" value="Drug Accusition">
Assult<input type="checkbox" name="caseType[]" value="Assult">
then in javascript i added this part:
var t=0;
var c=addClients['caseType[]'];
for(var i=0;i<c.length;i++)
c[i].checked?t++:null;
where addClientsis the name of my form.
then I tried PHP, which I embedded, and it listed the values as it should.