How to get the sum of selected checkboxes?

Discussion in 'Programming' started by redeagle1191, May 28, 2012.

  1. #1
    How to get the sum of selected checkboxes?

    
    <!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=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    
    <body>
    
    
                  <?php
    // Code downloaded from html-form-guide.com
    // This code may be used and distributed freely without any charge.
    //
    // Disclaimer
    // ----------
    // This file is provided "as is" with no expressed or implied warranty.
    // The author accepts no liability if it causes any damage whatsoever.
    //
    
    
        if(isset($_POST['formSubmit'])) 
        {
            $aDoor = $_POST['formDoor'];
            
            
            if(empty($aDoor)) 
            {
                echo("<p>not selected</p>\n");
            } 
            else 
            {
                $N = count($aDoor);
    
    
                echo("<p>you select $N box <br>");
                for($i=0; $i < $N; $i++)
                {
                    echo($aDoor[$i] . " ");
                }
                
                echo("</p>");
            }
            
            
            
            //and so on
        }
        
        
    ?>
    
    
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
        <p>
            Which buildings do you want access to?<br/>
            <input type="checkbox" alt="gio" name="formDoor[]" value="10" />Acorn Building<br />
            <input type="checkbox" name="formDoor[]" value="20" />Brown Hall<br />
            <input type="checkbox" name="formDoor[]" value="30" />Carnegie Complex<br />
            <input type="checkbox" name="formDoor[]" value="40" />Drake Commons<br />
            <input type="checkbox" name="formDoor[]" value="50" />Elliot House
        </p>
        
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
    
    
    
    
    
    
    
    
    </body>
    </html>
    
    PHP:
     
    Solved! View solution.
    redeagle1191, May 28, 2012 IP
  2. #2
    
    $total = count(array_keys($aDoor, '1'));
    
    PHP:
     
    kbduvall, May 28, 2012 IP
  3. redeagle1191

    redeagle1191 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    and now I want to my code print checked box value and file name or alt
    ext:
      <input type="checkbox" name="formDoor[]" alt="book" value="20" />Brown Hall<br /> 
    HTML:
    print: book - 20 or Brown Hall - 20 if it possible.

    plz help
     
    redeagle1191, May 29, 2012 IP