Forum Moderators: coopster

Message Too Old, No Replies

Checkbox validation

         

osemollie

2:37 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



Dear colleagues,

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">
&nbsp;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">
&nbsp;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.

Scally_Ally

3:09 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



I think it may be better to do this sort of validation using javascript..

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.

coopster

5:35 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I agree that client-side validation is always nice, saves the user a round-trip to the server, but I would also say that you should ALWAYS do server-side validation. You don't know when a user may have JavaScript disabled in their browser.
I typically do both, provides convenience to the end user by validating before we have to hit the server again, and then server-side to make sure we are getting what we expect.

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.

osemollie

12:28 pm on Feb 18, 2006 (gmt 0)

10+ Year Member



Thanks for your answer. I have now separated and renamed the checkboxes to reflect the two weeks. But now, how do I validate the checkboxes?

coopster

3:39 pm on Feb 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Let's have a look at your editing criteria again:


- 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
}

You'll have to build on that, but it should get you started thinking in the right direction. Next one,
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
}

Now, using some of the logic demonstrated here, you show me how you would figure out the last edit ;)