Forum Moderators: coopster
Am working on a beginners project and I have problems with my checkboxes. These checkboxes are an array and I need to validate them.
- Students are supposed to choose 2 courses per week.
- However Financial Management is taught for a whole week hence if a student chooses this course in week 1, they are NOT allowed to choose another course in that particular week.
- If a student chooses Financial Management in week 1, they MUST choose it again in week 2.
How do I validate these?
_________________________
The code for the table looks like:
____________________
<html>
<head>
</head>
<body>
<form method="POST" action="insert.php">
<table border="0" width="500" id="table1" cellspacing="0" cellpadding="0">
<tr>
<td width="397"><b>Course Name</b></td>
<td width="103"><strong>
Select</strong></td>
</tr>
<tr>
<td width="500" bgcolor="#C0C0C0" colspan="2"><b>WEEK1</b></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Financial Management</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Financial Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Product Marketing
</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Product Marketing"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Loan
Portfolio Audit Toolkit</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value=" Loan Portfolio Audit Toolkit"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Process Mapping </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value=" Process Mapping"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#C0C0C0"><b>WEEK 2</b></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Financial Management (continued.) </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Financial Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Strategic Marketing </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Strategic Marketing"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Risk Management </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Risk Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Pilot Testing </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Pilot Testing "></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
________________________________
Thanks.
You could then validate it without having the need to submit the form, and you could alert the user by way of alert boxes.. Or make it so when they pick financial management that the other financial management checkbox is automatically checked.
osemollie, you have written it out in plain words, now all you have to do is apply the code to those instructions. I think the first thing you may want to do is assign separate "course" arrays to separate the weeks. Something like:
<input type="checkbox" name="chkCourseWeek1[]" value="Financial Management">
... and in week 2 ...
<input type="checkbox" name="chkCourseWeek2[]" value="Financial Management">
Now you will be able to see if "Financial Management" is selected in week one and if so it should be the only entry for that array.
- Students are supposed to choose 2 courses per week.
- However Financial Management is taught for a whole week hence if a student chooses this course in week 1, they are NOT allowed to choose another course in that particular week.
- If a student chooses Financial Management in week 1, they MUST choose it again in week 2.
OK, the first one. We need to make sure there have been at least two entries per week, right? So, we know that the checkboxes are going to be returned as arrays. If we don't have at least two entries per, then we have our first possible error.
if (count [php.net]($_POST [php.net]['chkCourseWeek1']) < 2) {
// Students are supposed to choose 2 courses per week
}
if (in_array [php.net]('Financial Management', $_POST['chkCourseWeek1']) && [php.net] count [php.net]($_POST [php.net]['chkCourseWeek1']) > 1) {
// Financial Management is taught for a whole week hence
// you are NOT allowed to choose another course this week
}