--------------------------------------------------------------------------
HOWTO : Information on using arrays with forms  (part 1 of n)            :
--------------------------------------------------------------------------

A few array/form questions have popped up recently so here begins a HOWTO 
on dealing with them.  We'll be using $form within our examples so without
further ado, let's talk a little about arrays and forms.

Arrays can be called as usual, such as :

  <input type="text" name="form[]">
  <input type="text" name="form[]">
  <input type="text" name="form[]">

To create this with PHP use a for loop OR quickly try this :

  $max = 3;
  echo str_repeat('<input type="text" name="form[]">',$max);
  
When submitted by the user this will create an array with 3 elements
named 0-2, it'll look something like :

  $form = array( 0 => 'philip', 1 => '[EMAIL PROTECTED]', 2 => 'me.com' );
  
Also, associative arrays work with forms, like this :

  <input type="text" name="form[name]">
  <input type="text" name="form[email]">
  <input type="text" name="form[url]">

Here's a quick simple way to create this form with PHP :

  $names = array('name','email','url');
  foreach ($names as $name) {
    echo '<input type="text" name="form['. $name .']">';
  }

Upon submittion this gives us an associative array similar to :

  $form = array( 'name'=>'philip', 'email'=>'[EMAIL PROTECTED]', 'url'=>'me.com' );
  
In both cases, our form processor reads this submitted array and does its
thing.  It's here we get creative and make the user well aware of what's
happening and what needs to happen.  Doing so means printing existing
values within the form when resubmition is required.  We want to inform
the user what went wrong (not long enough, must be alphanumeric, etc.) and
what to do about it (<b>fill this out!</b>).  We'll deal with this later.

It's good to know about these (and other) PHP and Environment variables :

  REQUEST_METHOD   = What method the form has made, such as POST and GET
  HTTP_POST_VARS   = Array of POSTed information
  HTTP_GET_VARS    = Array of GETed information
  QUERY_STRING     = String of GET information ?a=b&c=d&e=f
  GLOBALS          = An array that contains all global variables.  This is
                     freely available within functions too.

More are seen through a call to phpinfo() and listed here :

  http://www.php.net/manual/en/language.variables.predefined.php

It's also good to know about these (and other) settings :
  
  register_globals = When enabled, variables automatically enter the 
                     GLOBAL space. If enabled, $form can be called
                     directly otherwise the variables are to be read by
                     other means, like with HTTP_GET_VARS and
                     QUERY_STRING.  Something like this :

                         $form_data_array = $HTTP_POST_VARS['form'];

  magic_quotes_gpc = If on, data is escaped automagically on Get,Post and
                     Cookies (GPC).  This can also be done manually with 
                     addslashes() and if data is viewed while slashes
                     still exist use stripslashes() to strip them first.
                     Also get_magic_quotes_gpc() tells us the current
                     setting.

Read about php.ini settings here :

  http://www.php.net/manual/en/configuration.php
  
Current settings can be seen through phpinfo().  In our next installment 
(when?) we'll talk a little about processing our form arrays.

All words welcome.

Regards,
Philip



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to