php in multiple checkbox only get checked values not working code example

Example: get multiple checkbox value in php

To pass the multiple checkbox values in one POST action in PHP you can refer below code:

Html form code
<form method="post" action="send_data.php">
    <input type="checkbox" name="username[]" value="user1">User1
    <input type="checkbox" name="username[]" value="user2">User2
    <input type="checkbox" name="username[]" value="user3">User3
    <input type="checkbox" name="username[]" value="user4">User4
    <input type="submit" name="submit_data"/>
</form>
send_data.php
<?php
if(isset($_POST["submit_data"])){
   if(!empty($_POST["username"])){
       // to check the username checkboxes values you can use loop to display each checkbox value
       $usernames = $_POST["username"];
       foreach($usernames as $user){
            echo "User Name :".$user;
       }
   } 
}
?>

Tags:

Php Example