How to Get Values from Multiple Checkboxes Using PHP and $_POST

In PHP, $_POST is an associative array used to collect form data sent via the HTTP POST method. Here’s how you can create a simple HTML form with multiple checkboxes and retrieve their values using PHP.

Step 1: Create the HTML Form
You need to create a form that allows users to input their name, email, contact number, and select multiple skills using checkboxes. Here’s the HTML structure for that:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Get Values from Multiple Checkboxes in PHP</title> 
</head> 
<body> 
    <form action="process.php" method="post"> 
        <div> 
            <label>Name :</label> 
            <input type="text" name="name" required> 
        </div> 

        <div> 
            <label>Email :</label> 
            <input type="email" name="email" required> 
        </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" required> 
        </div> 

        <div> 
            <button type="submit">Submit</button> 
        </div> 
    </form> 
</body> 
</html>

Key Points:
name=”skills[]”: The square brackets ([]) after the name attribute allow multiple values (skills) to be collected as an array.
The method=”post” attribute ensures that the form data is sent via the POST method.
Step 2: Process the Submitted Data in PHP
Now, create a process.php file to process the form data using the $_POST superglobal. This will capture the name, email, contact, and skills selected by the user.

<?php 
// Get values from the form fields
$name = $_POST['name']; 
$email = $_POST['email']; 
$contact = $_POST['contact']; 

// Get the selected skills or assign an empty array if none are selected
$skills = isset($_POST['skills']) ? $_POST['skills'] : []; 
?> 

<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 
// Display selected skills or indicate no skills were chosen
if (count($skills) > 0) { 
    foreach ($skills as $skill) { 
        echo $skill . ' '; 
    } 
} else { 
    echo "No skill has been selected"; 
} 
?> 
</p>

Key Points:
isset() function: Checks if the skills checkbox array has any values. If not, an empty array is assigned to avoid errors.
Looping through skills: If at least one checkbox is selected, the script will loop through each value and display it.
Step 3: Test and Run
Create two files: index.html (for the form) and process.php (for handling the form submission).
Open index.html in your browser, fill in the form, and submit. The results will be displayed on the process.php page.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *