This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to get the checkbox values in PHP.
Let’s say you have 4 checkboxes of colours.
Get Checkbox Value in PHP
In order to get the checkbox values, you can use the foreach()
method.
<html>
<body>
<form method="post" action="">
<span>What are your favourite colours?</span><br/>
<input type="checkbox" name='colour[]' value="Red"> Red <br/>
<input type="checkbox" name='colour[]' value="Green"> Green <br/>
<input type="checkbox" name='colour[]' value="Blue"> Blue <br/>
<input type="checkbox" name='colour[]' value="Black"> Black <br/>
<br/>
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['colour'])) {
foreach($_POST['colour'] as $value){
echo "Chosen colour : ".$value.'<br/>';
}
}
}
?>
</body>
</html>
Result
Note: The foreach()
method functions by looping through all checked checkboxes and displaying their values.
The post How to Get Checkbox Values in PHP appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-03-01T12:42:21+00:00) How to Get Checkbox Values in PHP. Retrieved from https://www.scien.cx/2021/03/01/how-to-get-checkbox-values-in-php/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.