PHP Tips – Using Arrays in Form Fields

Not only can you create a form field that creates an element in an array, but you can create dynamic arrays as well.

This is especially useful in checkboxes, where the user could check multiple options. Take a look at this html:

<label><input type="checkbox" name="hobbies[]" value="Sports" /> Sports</label><br />
<label><input type="checkbox" name="hobbies[]" value="Hiking" /> Hiking</label><br />
<label><input type="checkbox" name="hobbies[]" value="Swimming" /> Swimming</label><br />
<label><input type="checkbox" name="hobbies[]" value="Watching Movies" /> Watching Movies</label><br />

When the above fields are posted to a php page, each hobby is added to the hobbies array. You can then loop through that array and access each value that was checked.


$hobbies = ($_POST['hobbies'] ;

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>