Array is a collection of similar data elements, means we can use array to store similar elements referenced under a common name.
In web development sometimes we need to group HTML form elements as arrays do. For example, you have a form where user has to select his hobbies from a group of check boxes as given below.
<form action="showlist.php" method="post" name="hobby_frm"> <h3> Select your hobbies </h3> <input type="checkbox" name="hobbies" value="hb1" /> Reading <br /> <input type="checkbox" name="hobbies" value="hb2" /> Surfing <br /> <input type="checkbox" name="hobbies" value="hb3" /> Listening to music <br /> <input type="submit" value="submit" /> </form>Please notice the name of all checkboxes hobbies
The above code will define an array with the name hobbies
In PHP, data sent from a form with post method is available in $_POST array.
We can access hobbies using the following code.
$hobbies = $_POST['hobbies']; // use name of the array as key
foreach( $hobbies as $value )
courtesey webdevelopment hosting company cochin kerala