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 🙂

4 thoughts on “How to Get Values from Multiple Checkboxes Using PHP and $_POST

  • September 26, 2024 at 7:20 pm
    Permalink

    Thanks for ones marvelous posting! I seriously enjoyed reading it, you might be a great author.I will ensure that I bookmark your blog and will come back in the future. I want to encourage yourself to continue your great work, have a nice evening!

    Reply
  • October 2, 2024 at 4:47 pm
    Permalink

    I like the valuable information you provide in your articles. I’ll bookmark your blog and check again here frequently. I am quite certain I’ll learn plenty of new stuff right here! Best of luck for the next!

    Reply
  • October 3, 2024 at 3:36 am
    Permalink

    Its such as you learn my thoughts! You appear to grasp a lot approximately this, such as you wrote the book in it or something. I feel that you simply could do with a few p.c. to drive the message house a bit, but instead of that, this is wonderful blog. A fantastic read. I will definitely be back.

    Reply
  • October 6, 2024 at 5:10 pm
    Permalink

    I’ve learn several excellent stuff here. Definitely value bookmarking for revisiting. I surprise how much attempt you set to make any such excellent informative website.

    Reply

Leave a Reply

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