hi
in my registration page i got about 20 input fields everythings are working fine except this multiple checkboxes. i read the articles related to my isses but no joy yet becouse my code is very complecated for example the register.php datas will be validate and process by process.php and then send to session.php from here data will be adding through database.php, so its very complecated where to add what for that i will give all the page code that related to register.php

here is the register.php

<tr>
<td>Generation/Siblings::</td>
<td>
<p><input type="checkbox" name="generation[]" value="Mother"> Mother</p>
    <p><input type="checkbox" name="generation[]" value="Grand-mother"> Grand mother</p>
    <p><input type="checkbox" name="generation[]" value="Great-grandmother"> Great grandmother</p>
    <p><input type="checkbox" name="generation[]" value="Sisters"> Sisters</p>
    <p><input type="checkbox" name="generation[]" value="Daughters"> Daughters</p>

</td>
</tr>

here is the process.php

 /**
    * procRegister - Processes the user submitted registration form,
    * if errors are found, the user is redirected to correct the
    * information, if not, the user is effectively registered with
    * the system and an email is (optionally) sent to the newly
    * created user.
    */
   function procRegister(){
      global $database, $session, $form;
      $config = $database->getConfigs();

      /* Checks if registration is disabled */
      if($config['ACCOUNT_ACTIVATION'] == 4){
        $_SESSION['reguname'] = $_POST['user'];
        $_SESSION['regsuccess'] = 6;
        header("Location: ".$session->referrer);
      }

      /* Convert username to all lowercase (by option) */
      if($config['ALL_LOWERCASE'] == 1){
         $_POST['user'] = strtolower($_POST['user']);
      }
      /* Hidden form field captcha deisgned to catch out auto-fill spambots */
      if (!empty($_POST['killbill'])) { $retval = 2; } else {
      /* Registration attempt */
      $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['conf_pass'], $_POST['email'], $_POST['conf_email'], $_POST['phone'], $_POST['firstname'], $_POST['lastname'], $_POST['maidenname'], $_POST['dob'], $_POST['yearinscl'], $_POST['houseinscl'], $_POST['albatchyear'], $_POST['generation'], $_POST['address'], $_POST['telnomobile'], $_POST['telnooffice'], $_POST['profession'], $_POST['designation'], $_POST['nameemployer'], $_POST['typebusiness'], $_POST['employeradd'], $_POST['newsletteremail'], $_POST['sms'], $_POST['nameoncard'], $_POST['paymentmade'], $_POST['amountm'], $_POST['recieptnom'], $_POST['donation'], $_POST['amountd'], $_POST['recieptnod'], $_POST['postaladdress'], $_POST['postcost'], $_POST['status']);
      }

      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = 0;
         header("Location: ".$session->referrer);
      }
      /* E-mail Activation */
      else if($retval == 3){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = 3;
         header("Location: ".$session->referrer);
      }

here is the session.php

 /**
    * register - Gets called when the user has just submitted the
    * registration form. Determines if there were any errors with
    * the entry fields, if so, it records the errors and returns
    * 1. If no errors were found, it registers the new user and
    * returns 0. Returns 2 if registration failed.
    */
   function register($subuser, $subpass, $subconf_pass, $subemail, $subconf_email, $subphone, $subfirstname, $sublastname, $submaidenname, $subdob, $subyearinscl, $subhouseinscl, $subalbatchyear, $subgeneration, $subaddress, $subtelnomobile, $subtelnooffice, $subprofession, $subdesignation, $subnameemployer, $subtypebusiness, $subemployeradd, $subnewsletteremail, $subsms, $subnameoncard, $subpaymentmade, $subamountm, $subrecieptnom, $subdonation, $subamountd, $subrecieptnod, $subpostaladdress, $subpostcost, $substatus){
      global $database, $form, $mailer;  //The database, form and mailer object
      $token = $this->generateRandStr(16);  
      $config = $database->getConfigs();
      /* Username error checking */
      $field = "user";  //Use field name for username
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){
         $form->setError($field, "* Username not entered");
      }
      else{
         /* Spruce up username, check length */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < $config['min_user_chars']){
            $form->setError($field, "* Username below ".$config['min_user_chars']."characters");
         }
         else if(strlen($subuser) > $config['max_user_chars']){
            $form->setError($field, "* Username above ".$config['max_user_chars']."characters");
         }
         /* Check if username is not alphanumeric */
         else if(!preg_match("/^[a-z0-9]([0-9a-z_-\s])+$/i", $subuser)){        
            $form->setError($field, "* Username not alphanumeric");
         }
         /* Check if username is reserved */
         else if(strcasecmp($subuser, GUEST_NAME) == 0){
            $form->setError($field, "* Username reserved word");
         }
         /* Check if username is already in use */
         else if($database->usernameTaken($subuser)){
            $form->setError($field, "* Username already in use");
         }
         /* Check if username is banned */
         else if($database->usernameBanned($subuser)){
            $form->setError($field, "* Username banned");
         }
      }

      /* Password error checking */
      $field = "pass";  //Use field name for password
      if(!$subpass){
         $form->setError($field, "* Password not entered");
      }
      else{
         /* Spruce up password and check length*/
         $subpass = stripslashes($subpass);
         if(strlen($subpass) < $config['min_pass_chars']){
            $form->setError($field, "* Password too short");
         }
         /* Check if password is too long */
         else if(strlen($subpass) > $config['max_pass_chars'] ){
            $form->setError($field, "* Password too long");
         }
         /* Check if password is not alphanumeric */
         else if(!preg_match("/^([0-9a-z])+$/i", ($subpass = trim($subpass)))){
            $form->setError($field, "* Password not alphanumeric");
         }
          /* Check if passwords match */
         else if($subpass != $subconf_pass){
            $form->setError($field, "* Passwords do not match");
         }
      }

      /* Email error checking */
      $field = "email";  //Use field name for email
      if(!$subemail || strlen($subemail = trim($subemail)) == 0){
         $form->setError($field, "* Email not entered");
      }
      else{
         /* Check if valid email address using PHPs filter_var */
         if(!filter_var($subemail, FILTER_VALIDATE_EMAIL)){
            $form->setError($field, "* Email invalid");
         }
       /* Check if emails match, not case-sensitive */
         else if (strcasecmp($subemail, $subconf_email)){
            $form->setError($field, "* Email addresses do not match");
         }
         $subemail = stripslashes($subemail);   
      }

      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         return 1;  //Errors with form
      }
      /* No errors, add the new account to the database */
      else{
      $usersalt = $this->generateRandStr(8);    
      if($database->addNewUser($subuser, $subpass, $subemail, $token, $usersalt, $subphone, $subfirstname, $sublastname, $submaidenname, $subdob, $subyearinscl, $subhouseinscl, $subalbatchyear, $subgeneration, $subaddress, $subtelnomobile, $subtelnooffice, $subprofession, $subdesignation, $subnameemployer, $subtypebusiness, $subemployeradd, $subnewsletteremail, $subsms, $subnameoncard, $subpaymentmade, $subamountm, $subrecieptnom, $subdonation, $subamountd, $subrecieptnod, $subpostaladdress, $subpostcost, $substatus)){
        /* Check Account activation setting and process accordingly. */

        /* E-mail Activation */
        if($config['ACCOUNT_ACTIVATION'] == 2){
        $config = $database->getConfigs();
        $mailer->sendActivation($subuser,$subemail,$subpass,$token,$config);
        $successcode = 3;
        }

here is the database.php

/**
    * addNewUser - Inserts the given (username, password, email) info into the database. 
    * Appropriate user level is set. Returns true on success, false otherwise.
    */
   function addNewUser($username, $password, $email, $token, $usersalt, $phone, $firstname, $lastname, $maidenname, $dob, $yearinscl, $houseinscl, $albatchyear, $generation, $address, $telnomobile, $telnooffice, $profession, $designation, $nameemployer, $typebusiness, $employeradd, $newsletteremail, $sms, $nameoncard, $paymentmade, $amountm, $recieptnom, $donation, $amountd, $recieptnod, $postaladdress, $postcost, $status){
      $time = time();
      $config = $this->getConfigs();
      /* If admin sign up, give admin user level */
      if(strcasecmp($username, ADMIN_NAME) == 0){
         $ulevel = ADMIN_LEVEL;
      /* Which validation is on? */
      }else if ($config['ACCOUNT_ACTIVATION'] == 1) {
         $ulevel = REGUSER_LEVEL; /* No activation required */
      }else if ($config['ACCOUNT_ACTIVATION'] == 2) {
         $ulevel = ACT_EMAIL; /* Activation e-mail will be sent */
      }else if ($config['ACCOUNT_ACTIVATION'] == 3) {
         $ulevel = ADMIN_ACT; /* Admin will activate account */   
      }

     $password = sha1($usersalt.$password);
     $userip = $_SERVER['REMOTE_ADDR'];



     $query = "INSERT INTO ".TBL_USERS." SET username = :username, password = :password, usersalt = :usersalt, userid = 0, userlevel = $ulevel, email = :email, timestamp = $time, actkey = :token, ip = '$userip', regdate = $time, phone = :phone, firstname = :firstname, lastname = :lastname, maidenname = :maidenname, dob = :dob, yearinscl = :yearinscl, houseinscl = :houseinscl, albatchyear = :albatchyear, generation = :generation, address = :address, telnomobile = :telnomobile, telnooffice = :telnooffice, profession = :profession, designation = :designation, nameemployer = :nameemployer, typebusiness = :typebusiness, employeradd = :employeradd, newsletteremail = :newsletteremail, sms = :sms, nameoncard = :nameoncard, amountm = :amountm, recieptnom = :recieptnom, donation = :donation, amountd = :amountd, recieptnod = :recieptnod, postaladdress = :postaladdress, postcost = :postcost, status = :status";
     $stmt = $this->connection->prepare($query);

     return $stmt->execute(array(':username' => $username, ':password' => $password, ':usersalt' => $usersalt, ':email' => $email, ':token' => $token, ':phone' => $phone, ':firstname' => $firstname, ':lastname' => $lastname, ':maidenname' => $maidenname, ':dob' => $dob, ':yearinscl' => $yearinscl, ':houseinscl' => $houseinscl, ':albatchyear' => $albatchyear, ':generation' => $generation, ':address' => $address, ':telnomobile' => $telnomobile, ':telnooffice' => $telnooffice, ':profession' => $profession, ':designation' => $designation, ':nameemployer' => $nameemployer, ':typebusiness' => $typebusiness, ':employeradd' => $employeradd, ':newsletteremail' => $newsletteremail, ':sms' => $sms, ':nameoncard' => $nameoncard, ':amountm' => $amountm, ':recieptnom' => $recieptnom, ':donation' => $donation, ':amountd' => $amountd, ':recieptnod' => $recieptnod, ':postaladdress' => $postaladdress, ':postcost' => $postcost, ':status' => $status));
   }

You have set up your checkbox's as a control array, which means that $_POST['generation'] will itself be an array (not a single value) and must be iterated to find the selected value for insertion in the database. You will need some additional logic to handle this, you can't just pass the array to the SQL.

for additional logic do you mean like this in database.php

if(isset($_REQUEST['submit']))
    {

        $checkbox1 = $_POST['generation'];
        $selected_checkbox = "";
        foreach ($checkbox1 as $checkbox1) 
        {
            $selected_checkbox .= $checkbox1 . ", ";
        }
        $selected_checkbox = substr($selected_checkbox, 0, -2);

        $s="insert into <tablename> set
                area_manu='".$selected_checkbox."'";
    }

i tried this but still the inserting value is 0
any idea

Member Avatar for diafol

If you have 20 checkboxes and these are not going to change (as in loads more), one trick I ran into was using bitwise operators:

<input type="checkbox" value="1" name="checkbit[]" id="checkbit1" /> <label for="checkbit1">Check 1</label>
<input type="checkbox" value="2" name="checkbit[]" id="checkbit2" /> <label for="checkbit1">Check 2 </label>
<input type="checkbox" value="4" name="checkbit[]" id="checkbit3" /> <label for="checkbit1">Check 3</label>
<input type="checkbox" value="8" name="checkbit[]" id="checkbit4" /> <label for="checkbit1">Check 4</label>

(etc) - get php to create these in a loop if they're all together

$total = (isset($_POST['checkbit'])) ? array_sum($_POST['checkbit']) : 0;

THat gives the number you store in the DB.

For retrieval for example you can use - e.g. total now in variable $bitTotal:

if($bitTotal & 64) echo "checkbox #7 checked!"; 

The bitwise operator works in SQL too. SO you can search with it e.g.

SELECT ... FROM ...  WHERE (checkfields & 64)  
commented: Interesting usage of bitwise operators +9

i tryied with the above one but still its shows 0 value in db

i add this line in database.php

$total = (isset($_POST['generation'])) ? array_sum($_POST['generation']) : 0;
Member Avatar for diafol

do you have form tags? SHow the complete html.

Here is the register.php

<?php

include("include/session.php");
$config = $database->getConfigs();
?>

<!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>
<title><?php echo $config['SITE_NAME']; ?> - Register Page</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />

<?php 
$id = isset($_GET['id']) ? $_GET['id'] : 1;
if ($config['ENABLE_CAPTCHA']){
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="captcha/jquery/QapTcha.jquery.css" type="text/css" />
<?php 
} 
?>

</head>
<body>





<div id="container">
  <div id="header">
    <h1>METHODISTCOLLEGE.COM</h1>
  </div>
  <div id="navigation">
    <ul class="menu">
          <center><?php echo "<a href=".$config['WEB_ROOT'].$config['home_page'].">Back to Home Page</a>"; ?>
</center>
    </ul>
  </div>
  <div id="content">
    <div class="left">
<?php
/**
 * The user is already logged in, not allowed to register.
 */
if($session->logged_in){
   echo "<h1>Registered</h1>";
   echo "<p>We're sorry <b>$session->username</b>, but you've already registered. "
       ."<a href=\"index.php\">Main</a>.</p>";
}
/**
 * The user has submitted the registration form and the
 * results have been processed.
 */

    else if(isset($_SESSION['regsuccess'])){

    if ($_SESSION['regsuccess']==6){
      echo "<h1>Registration is currently disabled!</h1>";
      echo "<p>We're sorry <b>".$_SESSION['reguname']."</b> but registration to this site is currently disabled."
          ."<br>Please try again at a later time or contact the website owner.</p>";
    }
    /* Registration was successful */
    else if($_SESSION['regsuccess']==0 || $_SESSION['regsuccess']==5){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
          ."you may now <a href=\"index.php\">log in</a>.</p>";
    }
    else if($_SESSION['regsuccess']==3){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your account has been created. "
          ."However, this board requires account activation, an activation key has been sent to the e-mail address you provided. "
          ."Please check your e-mail for further information.</p>";
    }
    else if($_SESSION['regsuccess']==4){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your account has been created. "
          ."However, this board requires account activation by an Admin. An e-mail has been sent to them and you will be informed "
          ."when your account has been activated.</p>";
   }
   /* Registration failed */
   else if ($_SESSION['regsuccess']==2){
      echo "<h1>Registration Failed</h1>";
      echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
          ."could not be completed.<br>Please try again at a later time.</p>";
   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
   } 
    else if ((isset($_GET['mode'])) && ($_GET['mode'] == 'activate')) {
    $user = $_GET['user'];
    $actkey = $_GET['activatecode'];

    $sql = $database->connection->prepare("UPDATE ".TBL_USERS." SET USERLEVEL = '3' WHERE username=:user AND actkey=:actkey");
    $sql->bindParam(":user",$user);
    $sql->bindParam(":actkey",$actkey);
    $sql->execute();

    echo 'Your account is now activated.';
    // some warning if not successful
}
/**
 * The user has not filled out the registration form yet.
 * Below is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
else{
?>

<h1>Register</h1>
<?php
if($form->num_errors > 0){
   echo "<td style=\"color:#ff0000\">".$form->num_errors." error(s) found</td>";
}
?>
<form action="process.php" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>Status::</td>
<td><select name="status">
            <option value="Active">Active</option>
            <option value="Deceased">Deceased</option>
            <option value="Mail-returned">Mail returned</option>
            <option value="Overseas">Overseas</option>
          </select>
 </td>
</tr>
 <tr>
    <td>Username:</td>
    <td><input type="text" name="user" value="<?php echo $form->value("user"); ?>" /></td>
    <td><?php echo $form->error("user"); ?></td>
</tr>
<tr>
    <td>Password:</td>
    <td><input type="password" name="pass" value="<?php echo $form->value("pass"); ?>" /></td>
    <td><?php echo $form->error("pass"); ?></td></tr>
<tr>
    <td>Confirm password:</td>
    <td><input type="password" name="conf_pass" value="<?php echo $form->value("conf_pass"); ?>" /></td>
    <td><?php echo $form->error("pass"); ?></td>
</tr>
<tr>
    <td>Email address:</td>
    <td><input type="text" name="email" value="<?php echo $form->value("email"); ?>" /></td>
    <td><?php echo $form->error("email"); ?></td>
</tr>
<tr>
    <td>Confirm email address:</td>
    <td><input type="text" name="conf_email" value="<?php echo $form->value("conf_email"); ?>" /></td>
    <td><?php echo $form->error("email"); ?></td>
</tr>
<tr>
<td>First Name::</td>
<td><input type="text" name="firstname" size="30" maxlength="30" value="<?php echo $form->value("firstname"); ?>" /></td>
</tr>
<td>Last Name::</td>
<td><input type="text" name="lastname" size="30" maxlength="30" value="<?php echo $form->value("lastname"); ?>" /></td>
</tr>
<tr>
<td>Maiden Name::</td>
<td><input type="text" name="maidenname" size="30" maxlength="30" value="<?php echo $form->value("maidenname"); ?>" /></td>
</tr>
<tr>
<td>Date of Birth::</td>
<td><input type="text" name="dob" value="<?php echo date('Y-m-d'); ?>" /></td>
</tr>
<tr>
<td>Years in School::</td>
<td><select name="yearinscl">
            <option value="01">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option></select></td>
</tr>
<tr>
<td>House in School::</td>
<td><select name="houseinscl" id="select8">
            <option value="choate">Choate</option>
            <option value="park">Park</option>
            <option value="resterick">Resterick</option>
            <option value="rigby">Rigby</option>
            <option value="scott">Scott</option>
            <option value="shire">Shire</option>
          </select></td>
</tr>
<tr>
<td>A/L Batch Year::</td>
<td><select name="albatchyear">
            <option value="2012">2012</option>
            <option value="2011">2011</option>
            <option value="2010">2010</option>
            <option value="2009">2009</option>
            <option value="2008">2008</option>
            <option value="2007">2007</option>
            <option value="2006">2006</option>
            <option value="2005">2005</option>
            <option value="2004">2004</option>
            <option value="2003">2003</option>
            <option value="2002">2002</option>
            <option value="2001">2001</option>
            <option value="2000">2000</option>
            <option value="1999">1999</option>
            <option value="1998">1998</option>
            <option value="1997">1997</option>
            <option value="1996">1996</option>
            <option value="1995">1995</option>
            <option value="1994">1994</option>
            <option value="1993">1993</option>
            <option value="1992">1992</option>
            <option value="1991">1991</option>
            <option value="1990">1990</option>
            <option value="1989">1989</option>
            <option value="1988">1988</option>
            <option value="1987">1987</option>
            <option value="1986">1986</option>
            <option value="1985">1985</option>
            <option value="1984">1984</option>
            <option value="1983">1983</option>
            <option value="1982">1982</option>
            <option value="1981">1981</option>
            <option value="1980">1980</option>
             </select></td>
</tr>
<tr>
<td>Generation/Siblings::</td>
<td>
<input type="checkbox" value="1" name="generation[]" id="generation1" /> <label for="Mother">Mother</label>
<input type="checkbox" value="2" name="generation[]" id="generation2" /> <label for="Grand-mother">Grand mother</label>
<input type="checkbox" value="4" name="generation[]" id="generation3" /> <label for="Great-grandmother">Great grandmother</label>
<input type="checkbox" value="8" name="generation[]" id="generation4" /> <label for="Sisters">Sisters</label>
<input type="checkbox" value="16" name="generation[]" id="generation5" /> <label for="Daughters">Daughters</label>
</td>
</tr>
<tr>
<td>Address::</td>
<td>
<TEXTAREA NAME="address" ROWS=3 COLS=30 value="<?php echo $form->value("address"); ?>" ></TEXTAREA></td>
</tr>
<tr>
<td>Telephone::</td>
<td><input type="text" name="phone" size="14" maxlength="14" value="<?php echo $form->value("phone"); ?>" /></td>
</tr>
<tr>
<td>Telephone Mobile::</td>
<td><input type="text" name="telnomobile" maxlength="20" value="<?php echo $form->value("telnomobile"); ?>" /></td>
</tr>
<tr>
<td>Telephone Office::</td>
<td><input type="text" name="telnooffice" maxlength="20" value="<?php echo $form->value("telnooffice"); ?>" /></td>
</tr>
<tr>
<td>Profession/Occupation::</td>
<td><select name="profession">
            <option value="Architecture-and-Engineering">Architecture and Engineering</option>
            <option value="Arts,Design,Entertainment,Sports,Media">Arts, Design, Entertainment, Sports, and Media</option>
            <option value="Building-and-Grounds-Cleaning-and-Maintenance">Building and Grounds Cleaning and Maintenance</option>
            <option value="Business-and-Financial-Operations">Business and Financial Operations</option>
            <option value="Community-and-Social-Service">Community and Social Service</option>
            <option value="Computer-and-Mathematical">Computer and Mathematical</option>
            <option value="Construction-and-Extraction">Construction and Extraction</option>
            <option value="Education,Training,and-Library">Education, Training, and Library</option>
            <option value="Farming,Fishing,and-Forestry">Farming, Fishing, and Forestry</option>
            <option value="profession-10">Food Preparation and Serving</option>
            <option value="Food-Preparation-and-Serving">Healthcare Practitioners and Technical</option>
            <option value="Healthcare-Support">Healthcare Support</option>
            <option value="Installation,Maintenance,and-Repair">Installation, Maintenance, and Repair</option>
            <option value="Legal">Legal</option>
            <option value="Life,Physical,and-Social,Science">Life, Physical, and Social Science</option>
            <option value="Management">Management</option>
            <option value="Military-Specific">Military Specific</option>
            <option value="Office-and-Administrative-Support">Office and Administrative Support</option>
            <option value="Personal-Care-and-Service">Personal Care and Service</option>
            <option value="Production/Manufacturing">Production/Manufacturing</option>
            <option value="Protective-Service">Protective Service</option>
            <option value="Sales-and-Related">Sales and Related</option>
            <option value="Transportation-and-Material-Moving">Transportation and Material Moving</option>
          </select>
 </td>
</tr>
<tr>
<td>Designation::</td>
<td><input type="text" name="designation" maxlength="20" value="<?php echo $form->value("designation"); ?>" /></td>
</tr>

<tr>
<td>Name of Employer::</td>
<td><input type="text" name="nameemployer" maxlength="20" value="<?php echo $form->value("nameemployer"); ?>" /></td>
</tr>
<tr>
<td>Type of Business::</td>
<td><input type="text" name="typebusiness" maxlength="20" value="<?php echo $form->value("typebusiness"); ?>" /></td>
</tr>
<tr>
<td>Employer Address::</td>
<td><input type="text" name="employeradd" maxlength="255" value="<?php echo $form->value("employeradd"); ?>" /></td>
</tr>
<tr>
<td>Display Name on Card::</td>
<td><input type="text" name="nameoncard" maxlength="20" value="<?php echo $form->value("nameoncard"); ?>" /></td>
</tr>
<tr>
<td>Donation::</td>
<td><input type="number" name="donation" maxlength="20" value="<?php echo $form->value("donation"); ?>" /></td>
</tr>
<tr>
<td>Donation Amount::</td>
<td><input type="number" name="amountd" maxlength="20" value="<?php echo $form->value("amountd"); ?>" /></td>
</tr>
<tr>
<td>Reciept No::</td>
<td><input type="number" name="recieptnod" maxlength="20" value="<?php echo $form->value("recieptnod"); ?>" /></td>
</tr>
<tr>
<td>Postal Address::</td>
<td><input type="text" name="postaladdress" maxlength="255" value="<?php echo $form->value("postaladdress"); ?>" /></td>
</tr>
<tr>
<td>Postal Cost::</td>
<td><input type="text" name="postcost" maxlength="20" value="<?php echo $form->value("postcost"); ?>" /></td>
</tr>
<tr>
<td>Notification Through Email::</td>
<td><input type="checkbox" name="newsletteremail" value="yes" /></td
></tr>
<tr>
<td>Notification Through SMS::</td>
<td><input type="checkbox" name="sms" value="yes" /></td>
</tr>
<?php 
if ($config['ENABLE_CAPTCHA']){
    echo "<tr><td colspan=\"2\"><div class=\"QapTcha\"></div></td></tr>";
}
?>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1" />
<input type="submit" value="Register!" id="submit" /></td></tr>
<tr><td colspan="2" align="left">
    <?php echo "<a href=".$config['WEB_ROOT'].$config['home_page'].">Back to Home Page</a>"; ?>
</td></tr>
</table>
<!-- The following div tag displays a hidden form field in an attempt at tricking automated bots. -->
<div style='display:none; visibility:hidden;'><input type='text' name='killbill' maxlength='50' /></div>
</form>

<?php 
if ($config['ENABLE_CAPTCHA']){
?>
<!-- QapTcha and jQuery files -->
<script type="text/javascript" src="captcha/jquery/jquery.js"></script>
<script type="text/javascript" src="captcha/jquery/jquery-ui.js"></script>
<script type="text/javascript" src="captcha/jquery/jquery.ui.touch.js"></script>
<script type="text/javascript" src="captcha/jquery/QapTcha.jquery.js"></script>
<script type="text/javascript">
        $('.QapTcha').QapTcha({});
    </script>
<?php
}
}
?>
      <h2>&nbsp;</h2>

        <p>&nbsp;</p>
      <p>&nbsp;</p>

    </div>
      </div>
</div>
<center><div id="footer"></div><div id="navigation">
    <ul class="menu">


    </ul>
  </div></center>
<div align=center>The Copyright Content Goes Here<a href='http://methodistcolege.com/'>link</a></div>







</body>
</html>
Member Avatar for diafol

I thought you had 20 checkboxes together. Sorry my mistake. In the case of your html, I probably wouldn't bother with the bitwise stuff, but could still be useful.

<input type="checkbox" value="1" name="generation[]" id="generation1" /> <label for="Mother">Mother</label>
<input type="checkbox" value="2" name="generation[]" id="generation2" /> <label for="Grand-mother">Grand mother</label>
<input type="checkbox" value="4" name="generation[]" id="generation3" /> <label for="Great-grandmother">Great grandmother</label>
<input type="checkbox" value="8" name="generation[]" id="generation4" /> <label for="Sisters">Sisters</label>
<input type="checkbox" value="16" name="generation[]" id="generation5" /> <label for="Daughters">Daughters</label>

The 'for' attribute needs to be equal to the id of the control it's referring to, e.g. 'generation1' or 'generation5', etc. Alternatively, you could do something like this:

<input type="checkbox" value="1" name="generation[]" id="mother" /> <label for="mother">Mother</label>
<input type="checkbox" value="2" name="generation[]" id="grandmother" /> <label for="grandmother">Grand mother</label>
<input type="checkbox" value="4" name="generation[]" id="greatgrandmother" /> <label for="greatgrandmother">Great grandmother</label>
<input type="checkbox" value="8" name="generation[]" id="sisters" /> <label for="sisters">Sisters</label>
<input type="checkbox" value="16" name="generation[]" id="daughters" /> <label for="daughters">Daughters</label>

With processing...

$total = (isset($_POST['generation'])) ? array_sum($_POST['generation']) : 0;

Maybe this would be better:

<input type="checkbox" name="generation[1]" id="mother" /> <label for="mother">Mother</label>
<input type="checkbox" name="generation[2]" id="grandmother" /> <label for="grandmother">Grand mother</label>
<input type="checkbox" name="generation[4]" id="greatgrandmother" /> <label for="greatgrandmother">Great grandmother</label>
<input type="checkbox" name="generation[8]" id="sisters" /> <label for="sisters">Sisters</label>
<input type="checkbox" name="generation[16]" id="daughters" /> <label for="daughters">Daughters</label>

And then...

$total = (isset($_POST['generation'])) ? array_sum(array_keys($_POST['generation'])) : 0;

In order to retrieve this for profile pages or db searches:

Let us assume mother, greatgrandmother and daughters = 1 + 4 + 16 = 21

Store this as 21 in the DB - int field (IMPT!!)

Oh, BTW while I remember, this method only works for up to 32 checkboxes in the array on 32-bit server. You should be OK though, :)

function checkMe($total,$checkValue)
{
    if((int)$total & (int)$checkValue) echo ' checked="checked";
}

So this could be...

<input type="checkbox" name="generation[1]" id="mother" <?php checkMe($total,1);?> /> <label for="mother">Mother</label>
    <input type="checkbox" name="generation[2]" id="grandmother" <?php checkMe($total,2);?> /> <label for="grandmother">Grand mother</label>
    <input type="checkbox" name="generation[4]" id="greatgrandmother" <?php checkMe($total,4);?> /> <label for="greatgrandmother">Great grandmother</label>
    <input type="checkbox" name="generation[8]" id="sisters" <?php checkMe($total,8);?> /> <label for="sisters">Sisters</label>
    <input type="checkbox" name="generation[16]" id="daughters" <?php checkMe($total,16);?> /> <label for="daughters">Daughters</label>

You'll probably find that you'll get an error on page load as $total isn't defined. In that case, just place a

$total = 0;

Before your form processing code. Ideally you'd build this into your form handling class.

Just an idea. Don't waste too much time on it.

commented: 2 +0

thank you for your reply and help
but still no joy, i made the changes on register.php and add following code into database.php still the filed value is 0 in the database

$total = (isset($_POST['generation'])) ? array_sum($_POST['generation']) : 0;

any idea?

Member Avatar for diafol

have you tried echoing various variables to see what they hold, e.g.

$x = (array) $_POST['generation']';
print_r($x);

@devarmagan,

!Warning! Script below is not tested. However, I am confident that it will help you isolate the problems you are having. If I don't over parenthesized or missed some semi-colons it should work as I intended it to be.

jUst a simple suggestion, why not test checkboxes in one page, to see how the recommended codes work..

For example, per Diafol's recommendation

 ? array_sum($_POST['generation']) : 0;

should work, but if you don't check if the POST is set (the entire form), then you will probably ended up 0..

Here is tester code that I think might work. Save this on a single php page called tester.php

<?php
    echo '<h4>Form submission Results</h4>';

    if(isset($_POST['submit'])&& (!empty($_POST['generation']))){

    echo 'Test One:  Array Sum Generation:  '.array_sum($_POST['generation']).'<br/> Test Two: ';

    print_r($_POST['generation']);
    }
    echo '<br/>';
    echo 'Test Three: '.(($_SERVER['REQUEST_METHOD']== 'POST')?((!empty($_POST['generation'])?array_sum($_POST['generation']) : 0 ))  :'Checkbox is empty');

    ?>
    <form method="post" action="">

    <input type="checkbox" value="1" name="generation[]" id="mother" /> <label for="mother">Mother</label>
    <br/>
    <input type="checkbox" value="2" name="generation[]" id="grandmother" /> <label for="grandmother">Grand mother</label>
    <br/>
    <input type="checkbox" value="4" name="generation[]" id="greatgrandmother" /> <label for="greatgrandmother">Great grandmother</label>
    <br/>
    <input type="checkbox" value="8" name="generation[]" id="sisters" /> <label for="sisters">Sisters</label>
    <br/>
    <input type="checkbox" value="16" name="generation[]" id="daughters" /> <label for="daughters">Daughters</label>
    <br/>
    <input type="submit" name="submit" value="submit">
    </form>

Although I haven't tested the codes above, I highly favor the result coming out from test two and three. If you want more testing procedures, please let us know.

The purpose of the script is to approximate the sum of the value submitted as proposed by Diafol. It is highly recommended to convert them into interger, before storing into the database.

have you tried echoing various variables to see what they hold, e.g.

i have changed the register.php and add the following code into database.php

$total = (isset($_POST['generation'])) ? array_sum($_POST['generation']) : 0;

thats it
i check the results in phpmyadmin after adding the new user.

Member Avatar for diafol

SO? Does it work?

SO? Does it work?

nop, still the value is 0
pls help me

Member Avatar for diafol

Have you done a print_r($_POST['generation']) as we've suggested?

if i am right this print_r($_POST['generation']) is to pull out the data from table right?

here is the code i am using to pull the data from table

<td><?php
if($form->value("generation") == ""){
   echo $req_user_info['generation'];
}
?>
Member Avatar for diafol

No not out the table - from the form!

ok then i add this line

$total = (isset($_POST['generation'])) ? array_sum($_POST['generation']) : 0;

in process.php
something around here

/**
    * procRegister - Processes the user submitted registration form,
    * if errors are found, the user is redirected to correct the
    * information, if not, the user is effectively registered with
    * the system and an email is (optionally) sent to the newly
    * created user.
    */
   function procRegister(){
      global $database, $session, $form;
      $config = $database->getConfigs();
**--->**     ** $total = (isset($_POST['generation'])) ? array_sum($_POST['generation']) : 0; <-----**

      /* Checks if registration is disabled */
      if($config['ACCOUNT_ACTIVATION'] == 4){
        $_SESSION['reguname'] = $_POST['user'];
        $_SESSION['regsuccess'] = 6;
        header("Location: ".$session->referrer);
      }

but still no joy
any solution

Member Avatar for diafol

NO, the $total is lost in function scope as the function doesn't return anything. I may be wrong though - is there any more to this function?

Member Avatar for diafol

OK... so what do you want to do next?...

Where do the other fields in your form get processed? Like 'status' and 'maidenname'?

i want to add the multiple checkbox value to databse first and display in there user profile page and admin as well. the perticular user and admin should be able to view and edit genaration value after login in. all the other fileds are working fine currently can view and edit by user and admin but checkbox value is not, for now i want only to add the value to database

Member Avatar for diafol

sorry on vacation only gott mobile access

hi diafol
have you got any solution to my problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.