How to Get $_POST from multiple check-boxes ?

$_POST is an array of variable names. The program given below illustrates how to write HTML structure for multiple valued checkbox and to get the values of multiple valued checkbox using $_POST in PHP.

Note: The name attribute of checkboxes must be the same name and must be initialized with an array. It can be done by using [] at the end of the name attribute of the checkbox.

Example:

  • Code: The page (index.html) containing a form having name, email, contact and skills as fields and method post. Please note that for skills, every checkbox input has skills[] in name attribute.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            How to Get $_POST from
            multiple checkboxes?
        </title>
    </head>
      
    <body>
        <form action="process.php" method="post">
            <div>
                <label>Name :</label>
                <input type="text" name="name">
            </div>
              
            <div>
                <label>Email :</label>
                <input type="email" name="email">
            </div>
              
            <div>
                <label>Skills :</label>
                <input type="checkbox" name="skills[]"
                            value="Java"> Java 
                  
                <input type="checkbox" name="skills[]" 
                            value="Php"> PHP
                  
                <input type="checkbox" name="skills[]"
                            value="Python"> Python
                  
                <input type="checkbox" name="skills[]"
                            value="JavaScript"> JavaScript
            </div>
              
            <div>
                <label>Contact :</label>
                <input type="number" name="contact">
            </div>
              
            <div>
                <button type="submit">Submit</button>
            </div>
        </form>
    </body>
      
    </html>

    chevron_right

    
    

  • Code: The page (process.php) where we are processing the data sended by user.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Get the value of name field
    // from $_POST array
    $name = $_POST['name'];
      
    // Get the value of contact field
    // from $_POST array
    $contact = $_POST['contact'];
      
    // Get the value of email field
    // from $_POST array
    $email = $_POST['email'];
      
    // Check if at least one skill has been checked, if
    // checked, then assign the array returned by
    // $_POST['skills'] to $skills variable otherwise
    // assign an empty array
    $skills = (isset($_POST['skills'])) ? $_POST['skills'] : array();
      
    ?>
      
    <h2>Confirmation</h2>
    <p><strong>Name :</strong> <?php echo $name; ?></p>
    <p><strong>Email :</strong> <?php echo $email; ?></p>
    <p><strong>Contact :</strong> <?php echo $contact; ?></p>
    <p><strong>Skills :</strong> 
    <?php 
      
    // Check if $skills array has at least one
    // element, if so, then iterate through 
    // each element and echo its value, otherwise
    // echo that no skill is selected
    if (count($skills) > 0) {
        foreach ($skills as $skill) { 
            echo $skill .' '
        
    } else {
        echo "No skill has been selected";
    }
    ?>
    </p>

    chevron_right

    
    

Output:

  • Before submission of form i.e. “index.html”
    Before Submission
  • After submission of form i.e. “process.php”
    After Submission



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.