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


Code
 
// checkbox-data
<html>
<head>
<title>PHP Get Data from Check Boxes</title>
</head>
<body>
<form action="data.php" method="post">
Name: <input type="text" name="name">
<p>What sports do you like</p>
<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

</p>
</form>
</body>
</html>

Filename: php / data.php


Code
 
<?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 ?