How to manadatory checkboxes without display Server error to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ankit.pandeyc012
Forum Newbie
Posts: 8
Joined: Sun Nov 14, 2010 11:25 pm

How to manadatory checkboxes without display Server error to

Post by ankit.pandeyc012 »

Code: Select all

<?php
echo $error_msg='';
if(isset($_POST['submit']))
{
$LoginId=$_POST['LoginId'];
$Password1=$_POST['Password1'];
$Password2=$_POST['Password2'];
$Name=$_POST['Name'];
$Age=$_POST['Age'];
$BloodGroup=$_POST['BloodGroup'];
$Sex=$_POST['Sex'];
$Qualification=$_POST['Qualification'];
$Email=$_POST['Email'];
$Address=$_POST['Address'];
$AboutYourself=$_POST['AboutYourself'];
$countCheck=count($_POST['checkbox']);
echo $countCheck;
//$checkbox=$_POST['checkbox'];
//$countCheck=count($checkbox);
    if(empty($LoginId)){echo 'Please enter Login Id';}
    elseif(empty($Password1)){echo 'Please enter Password';}
    elseif(empty($Password2)){echo 'Please confirm Password';}
    elseif($Password1!==$Password2){echo 'Password didn\'t match';}
        elseif(empty($Name)){echo 'Please enter Name';}
        elseif(empty($Age)){echo 'Please enter Age';}
        elseif(empty($Sex)){echo 'Please enter Sex';}
        elseif(empty($Qualification)){echo 'Please enter Qualification';}
        elseif(empty($Email)){echo 'Please enter Email';}
        elseif(empty($Address)){echo 'Please enter Address';}
        elseif(empty($AboutYourself)){echo 'Please enter About Yourself';}
        elseif($countCheck==0){ echo 'You have to register at least one activity.';}
        else
        {
        $dbc=mysqli_connect('localhost','root','','nog');
        $query="select * from registration where LoginId='$LoginId'";
        $result=mysqli_query($dbc,$query);
        if(mysqli_num_rows($result)==0)    
        {        
        $checkbox=$_POST['checkbox'];
        $countCheck=count($_POST['checkbox']);
        $reg_id=' ';
        for($i=0;$i<$countCheck;$i++)
        {
        $reg_id=$reg_id.$checkbox[$i].',';
        }
        $query="insert into registration (LoginId,Password,Name,Age,BloodGroup,Sex,Qualification,Email,Address,AboutYourself,Activity)values ('$LoginId',SHA('$Password1'),'$Name','$Age','$BloodGroup','$Sex','$Qualification','$Email','$Address','$AboutYourself','$reg_id')";
        $result=mysqli_query($dbc,$query) or die("Not Connect");
        
        echo ' Dear '.$Name.'.<br>Your Account is waiting for approval<br>';
        echo "<a href='RegistrationAndActivity.php'>Back to Home</a>";
        //header('Location: index.php');
        //    header('Location: Registration.php');
        }
        else
        {
        echo 'Dear '.$Name. ' <br>Account already exist with Login Id '.$LoginId.' <br>Try another Login Id';
        }}    
}        
?>

<html>
<body>
<?php
echo $error_msg; ?>
<h3>New User?</h3>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
 
Enter Login id<br><input type='text' name='LoginId' value='<?php if(!empty($LoginId))echo $LoginId;?>'/><br>
Enter Password<br><input type='password' name='Password1' value='<?php if(!empty($Password1))echo $Password1;?>'/><br>
Confirm Password<br><input type='password' name='Password2' value='<?php if(!empty($Password2))echo $Password2;?>'/><br>
Enter Name<br><input type='text' name='Name' value='<?php if(!empty($Name))echo $Name;?>'/><br>
Enter Age<br><HEAD>
   <SCRIPT language=Javascript>
      
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      
      
     
   </SCRIPT>
   </HEAD>
<INPUT onkeypress='return isNumberKey(event)' type='text' name='Age' value='<?php if(!empty($Age))echo $Age;?>'><br/>
Enter Blood<br><input type='text' name='BloodGroup'value='<?php if(!empty($BloodGroup))echo $BloodGroup;?>'/><br>
Enter Sex<br><input type='text' name='Sex'value='<?php if(!empty($Sex))echo $Sex;?>'/><br/>
Enter Qualification<br><input type='text' name='Qualification' value='<?php if(!empty($Qualification))echo $Qualification;?>'/><br>
Enter Email<br><input type='text' name='Email' value='<?php if(!empty($Email))echo $Email;?>'/><br>
Enter Address<br><input type='text' name='Address' value='<?php if(!empty($Address))echo $Address;?>'/><br>
About Yourself<br/><textarea rows='10' cols='22' name='AboutYourself' value='<?php if(!empty($AboutYourself))echo $AboutYourself;?>'></textarea><br/>

<?php echo"
<b>Select fields for which you want to register</b><br/>";

$dbc=mysqli_connect('localhost','root','','nog');
$query="select * from activity";
$result=mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result)){
$Title=$row['Title'];
$ActivityId=$row['ActivityId'];
echo $Title;
//echo $ActivityId;
echo "<input type='checkbox' name='checkbox[]' value=$ActivityId />";//value=$ActivityId tells ActivityId variable extracts with name="checkbox"
echo "<br/>";
}
echo "<input type='submit' value='Register' name='submit'/><br>";
    
echo "</form>
</body>
</html>";?>
Hi friends.........
In above code I want to manadatory the checkboxes which works properly as well but when user not select any checkbox it should display "You have to register at least one activity." according to above code but user get an extra server error also "Notice: Undefined index: checkbox in C:\wamp\www\NGOProject\RegistrationAndActivity.php on line 16
You have to register at least one activity. "
I don't want to display this extra server error "Notice: Undefined index: checkbox in C:\wamp\www\NGOProject\RegistrationAndActivity.php on line 16" to user...............
please help me ..........
Anyone???????????????
thanks in advance..................
Post Reply