Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2006, 12:37 PM   PM User | #1
osemollie
New to the CF scene

 
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
osemollie is an unknown quantity at this point
Checkbox validation

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.
osemollie is offline   Reply With Quote
Old 02-18-2006, 12:58 PM   PM User | #2
funhunter
New Coder

 
Join Date: Jan 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
funhunter is an unknown quantity at this point
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.
funhunter is offline   Reply With Quote
Old 02-18-2006, 01:02 PM   PM User | #3
osemollie
New to the CF scene

 
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
osemollie is an unknown quantity at this point
Thanks for your response. I am also a beginner. Can you write a code to validate this and we look at it together?
osemollie is offline   Reply With Quote
Old 02-18-2006, 04:08 PM   PM User | #4
goughy000
Regular Coder

 
goughy000's Avatar
 
Join Date: Nov 2005
Location: England
Posts: 415
Thanks: 0
Thanked 0 Times in 0 Posts
goughy000 is an unknown quantity at this point
insert.php

PHP Code:
<?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 == && $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 == && $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

Last edited by goughy000; 02-18-2006 at 04:10 PM..
goughy000 is offline   Reply With Quote
Old 02-19-2006, 08:34 AM   PM User | #5
funhunter
New Coder

 
Join Date: Jan 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
funhunter is an unknown quantity at this point
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?
funhunter is offline   Reply With Quote
Old 02-19-2006, 10:40 AM   PM User | #6
dragon
New Coder

 
dragon's Avatar
 
Join Date: May 2003
Location: Florida
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
dragon is an unknown quantity at this point
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.
__________________
http://www.dragonshobbies.com
dragon is offline   Reply With Quote
Old 02-19-2006, 12:50 PM   PM User | #7
goughy000
Regular Coder

 
goughy000's Avatar
 
Join Date: Nov 2005
Location: England
Posts: 415
Thanks: 0
Thanked 0 Times in 0 Posts
goughy000 is an unknown quantity at this point
Quote:
user can modify javascript validation functions?
Of course.. they can save the file to there computer, change anything javascript, and load it up again


Quote:
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
goughy000 is offline   Reply With Quote
Old 02-20-2006, 09:50 AM   PM User | #8
funhunter
New Coder

 
Join Date: Jan 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
funhunter is an unknown quantity at this point
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.
funhunter is offline   Reply With Quote
Old 02-21-2006, 02:21 PM   PM User | #9
degsy
Senior Coder

 
Join Date: Nov 2002
Location: North-East, UK
Posts: 1,265
Thanks: 0
Thanked 0 Times in 0 Posts
degsy is on a distinguished road
PHP Code:
<!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/lo...orm_php_js.php
degsy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:34 PM.


Advertisement
Log in to turn off these ads.