Get Data As An Array – Download PHP Example

by Hiroshi on June 23, 2008

in Arrays, For Loop, Scripts

Array & Loop Example

Suppose we have a form and we want from user to select from a list of check boxes and check his favorite choices, post that data as an array and we want to display that data by calling array and arranging it in a loop. Then for this purpose consider following example.

Form Code

<p>Arrays example</p>
<p>Check your favorite Movies:
</p>
<form action="nextpagename.php" method="post" name="array-form" id="array-form">
  <p>
    <input name="pick[]" type="checkbox" value="Harry Potter">
    Harry Potter<br />
    <input name="pick[]" type="checkbox" value="The Hobbit">
    The Hobbit<br />
    <input name="pick[]" type="checkbox" value="Lord Of Rings">
    Lord Of Rings<br />
    <input name="pick[]" type="checkbox" value="Matrix">
    Matrix<br />
    <input name="pick[]" type="checkbox" value="Happy Feet">
    Happy Feet<br />
    <input name="pick[]" type="checkbox" value="The Hell Boy">
    The Hell Boy<br />
    <input name="pick[]" type="checkbox" value="Hulk">
    Hulk</p>
  <p>
    <label>
    <input type="submit" name="button" id="button" value="Submit">
    </label>
</p>
</form>

PHP Code – Retrieve Data From Array and Display It Through A Loop

<?php
for ($i=0; $i<count($_POST['pick']);$i++)
	{
$displaydata=$_POST['pick'][$i]."<br />";
echo "$displaydata";
        }
?>

In the above example we have collected the data from check boxes in form of arrays named as pick[] and we have sent it to another PHP page where it is displayed through a for loop.

PHP Arrays Example Download

Related Posts

Previous post:

Next post: