get the value of all selected checkboxes php code example
Example 1: 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"])){
$usernames = $_POST["username"];
foreach($usernames as $user){
echo "User Name :".$user;
}
}
}
?>
Example 2: check box with value in php
<form method="post" action="">
<span>Select languages</span><br/>
<input type="checkbox" name='lang[]' value="PHP"> PHP <br/>
<input type="checkbox" name='lang[]' value="JavaScript"> JavaScript <br/>
<input type="checkbox" name='lang[]' value="jQuery"> jQuery <br/>
<input type="checkbox" name='lang[]' value="Angular JS"> Angular JS <br/>
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['lang'])) {
foreach($_POST['lang'] as $value){
echo "value : ".$value.'<br/>';
}
}
}
?>