PHP Get Data From Check Boxes - Code

PHP Multidimensional Arrays Previous Next PHP Get Data from Radio Buttons

Checkboxes let a user select ONE or MORE options of a limited number of choices. In this code asks a user for his name and what sports he likes playing. His choice of sports is then stored in an array sports. Then the value of each array element is printed.

Filename: php / checkbox-data.html

// checkbox-data
<title>PHP Get Data from Check Boxes</title>
<form action="data.php" method="post">
Name: <input type="text" name="name">
<p>What sports do you like</p>
<input type="checkbox" name="sports[]" value="Cricket"> Cricket
<input type="checkbox" name="sports[]" value="Football"> Football
<input type="checkbox" name="sports[]" value="Hockey"> Hockey
<input type="checkbox" name="sports[]" value="Basketball"> Basketball


Filename: php / data.php

$name = $_POST["name"];
$sport = $_POST["sports"];

forearch($sport as $key => $value){
 echo "$name likes $value .
"; } ?>

Running Step.

Type Url - https://localhost/php/checkbox-data.html

Tags: PHP Get Data From Check Boxes PHP Code Programming Language

Was this helpful ?