Working with Forms Data in PHP.


Form fields and their values are stored in the PHP $_POST[] super-global associative array.
Depending upon the current configuration of PHP on your server, all form fields may also be stored as individual global variables.
Because of this convention, you should maintain a variable name standard for the naming of your form fields.
<input type=text name="NameField" value="Tom Jones" />

<textarea name="InformationAndComments" rows=5 cols=60>
This is some data
</textarea>


echo $_POST['NameField'] . "<br />\n"; echo $_POST['InformationAndComments'] . "<br />\n";
You should use caution when defining form field names that do not adhere to the standard PHP variable naming conventions. When you define fields in this fashion in your HTML, PHP will "attempt" to convert the field names into a compatible PHP variable name.
<input type=text name="Name Field" value="Tom Jones" />

<textarea name="Information &%^$@#/ Comments" rows=5 cols=60>
This is some data
</textarea>


print_r ($_POST);
Array
(
    [Name_Field] => Tom Jones
    [Information_+%^$@#/_Comments] => This is some data

)
NOTE the conversion of spaces into underscores
Although using the global array references
$_POST['Information_+%^$@#/_Comments'] and
$GLOBALS['Information_+%^$@#/_Comments']
will work, an attempt to reference $Information_+%^$@#/_Comments
will result in a syntax error.
If you plan on using this type of complicated naming convention for form fields, you should not plan on referencing the fields as global variables. Most current configurations of PHP have this option turned off by default.

Techniques for detecting Forms Submission
It is common that a programmer will design a forms-based webpage so that it consists of a pure HTML-based webpage, and a separate PHP-based script that processes the forms data.
When the forms-based page itself contains PHP-generated information, such as remembering field values from a previously submitted form, maintaining separate scripts becomes tedious.
It is very simple, however, to determine if a page is referenced via a URL reference or is called by a script. A variety of techniques can be used to do this.
$_SERVER['REQUEST_METHOD']
This variable returns either 'GET' or 'POST', indicating the method the page was referenced. To determine if a script is called by pressing a form submission button, this simple test could be used:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Another method would be to merely determine the size of the global $_POST array. If it has one or more indices, there was at least one form field passed, indicating the script had to be called from the posting of a form.
if (count($_POST) > 0)

This therefore creates a very simple model to combine the display of a form, and the processing of the submitted form, all within the same script file:
<?php

if (count($_POST) == 0) {

  // display the initial display of the form here

  }
else {

  // process the submitted forms data here

  }
?> 

Schemes for Submission Button
Naming and Access
Forms can have any number of submission buttons, and therefore can cause different actions depending upon which button is actually pressed. There are two common techniques that can be used to easily identify which action you wish to perform based on the actual button pressed.

Same Name, Different Values
The first technique is to name each selection button the same name, and then specify a different value. In the PHP code, you could test the value of the corresponding $_POST element, which will indicate which button was actually pressed.
   
&lt;input type=submit name=DoIt value="Button 1"&gt; &lt;input type=submit name=DoIt value="Button 2"&gt; &lt;input type=submit name=DoIt value="Please, press me, won't you?"&gt;
switch ($_POST['DoIt']) {
  case 'Button 1' :
     // button 1 code
     break;
  case 'Button 2' :
     // button 2 code
     break;
  case 'Please, press me, won't you?' :
     // "Please, press me, won't you" code
     break;
  } // end switch
The minor disadvantage of this approach is that since the submit-type field always uses the value as the text displayed in the button, this may require long comparisons since the text must match exactly.

Different Names
The second approach is to choose a different, unique name for each selection button field. In your PHP code that processes the form, you would merely test for the presence of each of the submit fields; if one is present, it was the one that was pressed. Similar to checkboxes and radio buttons, submit buttons that aren't actually pressed send no data to the script, and therefore do not appear in the resultant $_POST array.
&lt;input type=submit name=Button1 value="Button 1"&gt; &lt;input type=submit name=Button2 value="Button 2"&gt; &lt;input type=submit name=Button3 value="Please, press me, won't you?"&gt;
if (isset($_POST['Button1'])) {
  // button 1 code
  }
elseif (isset($_POST['Button2'])) {
  // button 2 code
  }
elseif (isset($_POST['Button3'])) {
  // button 3 code
  }

Dealing with Multiple Selections
Multiple selection form fields pose an interesting problem based on how PHP processing form fields in general.
&lt;form method=post action="showFields.cgi"&gt; &lt;select name="Options" size=5 multiple&gt; &lt;option&gt;Option 1&lt;/option&gt; &lt;option&gt;Option 2&lt;/option&gt; &lt;option&gt;Option 3&lt;/option&gt; &lt;option&gt;Option 4&lt;/option&gt; &lt;option&gt;Option 5&lt;/option&gt; &lt;/select&gt; &lt;input type=submit name=GO value=GO&gt; &lt;/form&gt;
Since PHP stores all form fields and their values as indices in the $_POST array, it can't deal with multiple fields being sent with exactly the same name.
PHP therefore uses a special array notation to represent mutiple selections:
<form method=post action="showFields.cgi">
  <select name="Options[]" size=5 multiple>

    &lt;option&gt;Option 1&lt;/option&gt;
    &lt;option&gt;Option 2&lt;/option&gt;
    &lt;option&gt;Option 3&lt;/option&gt;
    &lt;option&gt;Option 4&lt;/option&gt;
    &lt;option&gt;Option 5&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type=submit name=GO value=GO&gt;
&lt;/form&gt;

$_POST = Array
(
    [Options] => Array
        (
            [0] => Option 2
            [1] => Option 3
        )

    [GO] => GO
)
This approach can also be used for other form fields as well. In addition, index values can be used rather than automatically generating a new, numerically indexed element.
&lt;form method=post action="showFields.cgi"&gt; &lt;input name="f1[Fred]" value="fred"&gt; &lt;input name="f1[John]" value="john"&gt; &lt;input name="f1[Alias for John]" value="sam"&gt;&lt;br /&gt; &lt;input type=checkbox name="f2[]" value="0 0"&gt; &lt;input type=checkbox name="f2[]" value="0 1"&gt; &lt;input type=checkbox name="f2[]" value="0 2"&gt;&lt;br /&gt; &lt;input type=checkbox name="f2[]" value="1 0"&gt; &lt;input type=checkbox name="f2[]" value="1 1"&gt; &lt;input type=checkbox name="f2[]" value="1 2"&gt; &lt;input type=submit name=GO value="DO IT"&gt; &lt;/form&gt;



$_POST = Array
(
    [f1] => Array
        (
            [Fred] => fred
            [John] => john
            [Alias for John] => sam
        )

    [GO] => DO IT
)
Multiple-dimensional arrays are also possible using this notation:
&lt;form method=post action="showFields.cgi"&gt; &lt;input type=checkbox name="f1[0][0]" value="set"&gt; &lt;input type=checkbox name="f1[0][1]" value="set"&gt; &lt;input type=checkbox name="f1[0][2]" value="set"&gt;&lt;br /&gt; &lt;input type=checkbox name="f1[1][0]" value="set"&gt; &lt;input type=checkbox name="f1[1][1]" value="set"&gt; &lt;input type=checkbox name="f1[1][2]" value="set"&gt; &lt;input type=submit name=GO value="DO IT"&gt; &lt;/form&gt;


$_POST = Array
(
    [f1] => Array
        (
            [0] => Array
                (
                    [1] => set
                )

            [1] => Array
                (
                    [2] => set
                )

        )

    [GO] => DO IT
)
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment