faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input : Array Form Variables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

21 of 23 people (91%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How do I print the contents of an array? ex. results of checkboxes, in an email message

Oct 26th, 2001 03:53
Christian Harberts, peichp@hotmail.com http://www.php.net/manual/en/function.list.php


Practical exercise : taking an array defined by checkboxes in a form, 
and printing them out in an orderly fashion in an email message.  

1. The form fills the array (a set of checkboxes always have the same 
name):

     <input type="checkbox" name="array[]" value="Item1">
     <input type="checkbox" name="array[]" value="Item2">

  When the form is posted, PHP considers the element name as a 
variable, holding the array : $array

2. A PHP script extracts the array values, then prints them in an 
orderly fashion.

A) Extract array values to a new variable, $print_array, like so:

     <? 
     while(list($key, $val) = each($array)) {
     $print_array .=$val .= " ";
     }
     ?>

  .= concatenates each value, $val. A string is created, with spaces 
  between each value, .= " "

B) Print the new variable, $print_array

     echo "Following are the values of the checkboxes: 
$print_array";  //if you want to print to the browser

[or]

     $messagetext="Following are the values of the checkboxes: 
$print_array"; //if you want to print to an email message

Either way, you would get:  

Following are the values of the checkboxes: Item1 Item2