...

View Full Version : Checkbox validation



osemollie
02-18-2006, 12:37 PM
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="chkCourseWeek1[]" 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="chkCourseWeek1[]" 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="chkCourseWeek1[]" 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="chkCourseWeek1[]" 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="chkCourseWeek2[]" 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="chkCourseWeek2[]" 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="chkCourseWeek2[]" 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="chkCourseWeek2[]" 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.

funhunter
02-18-2006, 12:58 PM
I think you can do this with javascript.By clicking on a button, you can check value of the check box for FM course, then uncheck all other checkbox in week1 and week2, and set the check box for FM in week2.
If FM course is not selected in week1 then just count the number of check box that is set, alert the user with appropriate message.If no error found then submit the form.I can code this idea, but I prefer to but different name for differrent checkbox so that I can access them easily with javascript.
I'm a beginner, dont know much, sorry.

osemollie
02-18-2006, 01:02 PM
Thanks for your response. I am also a beginner. Can you write a code to validate this and we look at it together?

goughy000
02-18-2006, 04:08 PM
insert.php



<?php

// Week1 stuff...
$week1 = $_POST[chkCourseWeek1];
$week1chosen = "0";

foreach($week1 AS $option){
if($option == "Financial Management"){
$maxweek1 = 1;
}
$week1chosen++;
}


// Week2 stuff...
$week2 = $_POST[chkCourseWeek2];
$week2chosen = "0";

foreach($week2 AS $option){
if($option == "Financial Management"){
$maxweek2 = 1;
}
$week2chosen++;
}


// Validate!

if($maxweek1 == 1 && $week1chosen > 1){
echo "You chose Financial Management in week 1, because of this you can only chose 1 course for week 1<br />";
exit();
}else{
echo "Week 1 is fine!<br>";
}


if($maxweek2 == 1 && $week2chosen > 1){
echo "You chose Financial Management in week 2, because of this you can only chose 1 course for week 2<br>";
exit();
}else{
echo "Week 2 is fine!<br />";
}

?>


Did it in PHP instead of javascript, javascript can be edited by the user so is less secure

funhunter
02-19-2006, 08:34 AM
oh, really?user can modify javascript validation functions?I wrote 12 validation function in PHP and then convert all of them to javascript ! it's not good ?:( I deleted all my PHP functions.But clientside checking is more convenient to user, is it?

dragon
02-19-2006, 10:40 AM
You can add client side validation via Javascript for quick interaction with the user (such as alerting them that a required field is not filled in), but you should still have server side validation in place. There have been cases which I've read about where online stores only had client side validation and ended up selling high priced items for $1. Also, don't forget that some people don't have Javascript enabled, so even if they aren't trying to hack it, the validation won't be there for them.

goughy000
02-19-2006, 12:50 PM
user can modify javascript validation functions?

Of course.. they can save the file to there computer, change anything javascript, and load it up again



but you should still have server side validation in place

Yes, I agree. As many validation proccesses as possible in place. And after data has been received its always best to check it by hand. But if you have lots of data received daily... ergh

funhunter
02-20-2006, 09:50 AM
Well, I got your idea, I change PHP to Javascript because I thought it will give some boost in performance, server will be free from validating, but I will think well next time, thanks.

degsy
02-21-2006, 02:21 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--//
function chkFrm(el){
if(el.name == "course"){
count = 0;
str = '';
for(x=0; x<el.elements["chkCourse[]"].length; x++){
if(el.elements["chkCourse[]"][x].checked==true){
str += el.elements["chkCourse[]"][x].value + ',';
count++;
}
}

if(count < 2){
alert("You must choose at least 2");
return false;
}
else if(count > 2){
alert("You can choose a maximum of 2");
return false;
}
else {
alert("You chose " + count + ": " + str.substring(0,str.length-1));
return true;
}
}

if(el.checked){
for(x=0; x<el.form.elements["chkCourse[]"].length; x++){
if(!el.form.elements["chkCourse[]"][x].id.indexOf('fm_wk')){
el.form.elements["chkCourse[]"][x].checked = true;
}
}
}
else{
for(x=0; x<el.form.elements["chkCourse[]"].length; x++){
if(!el.form.elements["chkCourse[]"][x].id.indexOf('fm_wk')){
el.form.elements["chkCourse[]"][x].checked = false;
}
}
}
}
//-->
</script>
</head>

<body>
<?php
if(isset($_POST['B1'])){
$count = 0;
$fm_count = 0;
$err = '';
if(is_array($_POST['chkCourse'])){
foreach($_POST['chkCourse'] as $key => $val){
if($val == "Financial Management"){
$fm_count++;
}
$count++;
}

if($fm_count == 1){
$err .= "You must choose Financial Management for both weeks<br>";
}
if($count > 2){
$err .= "You have chosen too many courses";
}
elseif($count < 2){
$err .= "You must choose atleast 2 courses<br>";
}

if(strlen($err) > 0){
echo $err;
}
else{
echo "Form OK!";
}
}
else{
echo "You must choose atleast 2 courses";
}
}
?>
<form action="<?=$_SERVER['SCRIPT_NAME']?>" method="post" name="course" id="course" onsubmit="return chkFrm(this)">
<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[]" id="fm_wk1" value="Financial Management" onclick="chkFrm(this)" /></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[]" id="fm_wk2" value="Financial Management" onclick="chkFrm(this)" /></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>

http://computer-helpforum.com/php/loop_checkbox_check_form_php_js.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum