How to get input from checkbox in php - If in input type radio button, the user can only choose one choice among several options. But in the form input type check box, the user may choose more than one option. To better understand the following I give examples of the application form input type check box.
Program 1
File Name: input07.php
Description: The program displays the form of input of favorite band name with check box.
Open notepad then type the following html code into notepad.
<html>
<head><title>Favorite food ~ Inputan Checkbox</title></head>
<body>
<FORM ACTION="proses07.php" METHOD="POST" NAME="input">
<h2>Choose your Favorite Food :</h2>
<input type="checkbox" name="food01" value="Fried rice" checked> Fried rice<br>
<input type="checkbox" name="food02" value="Grilled chicken"> Grilled chicken<br>
<input type="checkbox" name="food03" value="Meatballs"> Meatballs<br>
<input type="checkbox" name="food04" value="Noodles"> Noodles<br>
<input type="submit" name="Choose" value="Choose">
</FORM>
</body>
</html>
Save the code into the htdocs folder named input07.php
Program 2
File Name: process07.php
Description: Program to display the favorite band name according to the input in program 1.
Type the following php code into notepad.
<?php
if (isset($_POST['Choose'])) {
echo "Your favorite food is :<br>";
if (isset($_POST['food01'])) {
echo "+ " . $_POST['food01'] . "<br>";
}
if (isset($_POST['food02'])) {
echo "+ " . $_POST['food02'] . "<br>";
}
if (isset($_POST['food03'])) {
echo "+ " . $_POST['food03'] . "<br>";
}
if (isset($_POST['food04'])) {
echo "+ " . $_POST['food04'] . "<br>";
}
}
?>
Save the code into the htdocs folder named process07.php.
Explanation of Programs 1 and 2
Program 1 will display the preferred check box input box (see picture). In the form input type check box, the name of each check box should be distinguished. Users can select more than one option. Pay attention to program 1 line 6-9! To retrieve the value of the check box type form, it should be checked first whether the check box is selected or not, using the isset () function. Note the 2nd row program. If check box is checked (selected) then take value from check box (row 5).
To see the result, open the browser then in the address bar type http://localhost/input07.php then enter. Then the result will look like the following.
That's the tutorial we can share about how to get input from checkbox in php. Hopefully these tutorials can add to our knowledge of php programming language.
Program 1
File Name: input07.php
Description: The program displays the form of input of favorite band name with check box.
Open notepad then type the following html code into notepad.
<html>
<head><title>Favorite food ~ Inputan Checkbox</title></head>
<body>
<FORM ACTION="proses07.php" METHOD="POST" NAME="input">
<h2>Choose your Favorite Food :</h2>
<input type="checkbox" name="food01" value="Fried rice" checked> Fried rice<br>
<input type="checkbox" name="food02" value="Grilled chicken"> Grilled chicken<br>
<input type="checkbox" name="food03" value="Meatballs"> Meatballs<br>
<input type="checkbox" name="food04" value="Noodles"> Noodles<br>
<input type="submit" name="Choose" value="Choose">
</FORM>
</body>
</html>
Save the code into the htdocs folder named input07.php
Program 2
File Name: process07.php
Description: Program to display the favorite band name according to the input in program 1.
Type the following php code into notepad.
<?php
if (isset($_POST['Choose'])) {
echo "Your favorite food is :<br>";
if (isset($_POST['food01'])) {
echo "+ " . $_POST['food01'] . "<br>";
}
if (isset($_POST['food02'])) {
echo "+ " . $_POST['food02'] . "<br>";
}
if (isset($_POST['food03'])) {
echo "+ " . $_POST['food03'] . "<br>";
}
if (isset($_POST['food04'])) {
echo "+ " . $_POST['food04'] . "<br>";
}
}
?>
Save the code into the htdocs folder named process07.php.
Explanation of Programs 1 and 2
Program 1 will display the preferred check box input box (see picture). In the form input type check box, the name of each check box should be distinguished. Users can select more than one option. Pay attention to program 1 line 6-9! To retrieve the value of the check box type form, it should be checked first whether the check box is selected or not, using the isset () function. Note the 2nd row program. If check box is checked (selected) then take value from check box (row 5).
To see the result, open the browser then in the address bar type http://localhost/input07.php then enter. Then the result will look like the following.
![]() |
Program View |
![]() |
Program View |
That's the tutorial we can share about how to get input from checkbox in php. Hopefully these tutorials can add to our knowledge of php programming language.
Comments
Post a Comment