Form elements as PHP variables

I had already decided to test this today since I’m a lazy programmer (more on that sometime later). I’m working on creating a voting application and since I like code re-use (part of my laziness) I wanted to see how I could make form processing code that really doesn’t care how many different elements there are in the form. Eric’s wondernment on why PHP requires the brackets to be present in some form element names may also be explained a bit further.

As all PHP programmers know, and newbies generally learn pretty fast, PHP requires that form elements from which the values should end up in an array must have brackets ([]) in the end of the element’s name attribute (i.e. the elements ‘name’). While many other programming languages that can handle form input don’t require this approach, I feel PHP has some benefits.

First of all, in other languages the value that the programmer gets from a form with multiple elements with the same name is a string with all of the values separated by some character (generally a semi-colon or similar). While this works well for checkboxes etc. with values controlled by the programmer, a text input box would probably cause interesting results. To be honest, I don’t know if this is really the case. PHP solves this by requiring the brackets so no arbitrary separator characters are needed.

Secondly, and now we come to the interesting part, you can create multi-dimensional arrays in the form without any need for processing in the form handling code. For example, consider the following form:

<h2>category 1</h2>

<p>
URI: <input type="text" size="40" name="url['category1'][]" />
Title: <input type="text" size="40" name="title['category1'][]" />
<br />
URI: <input type="text" size="40" name="url['category1'][]" />
Title: <input " size="40" name="title['category1'][]" />
<br />
URI: <input "text" size="40" name="url['category1'][]" />
Title: <input type="text" size="40" " />
</p>

<h2>category 2</h2>

<p>
URI: <input type="text" "40" " />
Title: <input type="text" size="40" name="title['category2'][]" />
<br />
URI: <input type="text" size="40" name="url['category2'][]" />
Title: <input type="text" " name="title['category2'][]" />
<br />
URI: <input "text" "40" " />
Title: <input type="text" size="40" name="title['category2'][]" />
</p>

Now if a user fills out the form with some values (e.g. URL: http://example.com/category1/1/, Title: cat 1 blog 1, etc.) the following is what you see (with print_r($_REQUEST);) as the values in PHP:

Array
(
    [url] => Array
        (
            ['category1'] => Array
                (
                    [0] => http://example.com/category1/1/
                    [1] => http://example.com/category1/2/
                    [2] => http://example.com/category1/3/
                )

            ['category2'] => Array
                (
                    [0] => http://example.com/category2/1/
                    [1] => http://example.com/category2/2/
                    [2] => http://example.com/category2/3/
                )

        )

    [title] => Array
        (
            ['category1'] => Array
                (
                    [0] => cat 1 blog 1 
                    [1] => cat 1 blog 2
                    [2] => cat 1 blog 3
                )

            ['category2'] => Array
                (
                    [0] => cat 2 blog 1
                    [1] => cat 2 blog 2
                    [2] => cat 2 blog 3
                )

        )

    …
)

Now you can handle the data any way you want, but for the sake of an example we’ll look at how a list can be made of the submitted items (skipping items with an empty URL):

echo '<ul>';
foreach ($_REQUEST['url'] as $category=>$urls) {
  $titles = $_REQUEST['title'][$category];
  echo '<li>' . $category . '<ul>';
  for ($i = 0; $i < sizeof($urls); $i++) {
    if (empty($urls[$i])) continue;
    echo '<li><a href="' . $urls[$i] . '">';
    if (empty($titles[$i]))
      echo $urls[$i];
    else 
      echo $titles[$i];
    echo '</a></li>';
  }
  echo '</ul></li>';
}
echo '</ul>';

Perhaps in light of this example, the reason for PHP’s requirement of the brackets when an array is intended as the output of a form is more apparent.

One thought on “Form elements as PHP variables”

  1. The ability to add square brackets in order to allow extra array handling of form values, especially multi-dimensional arrays, is a nice feature. I do not dispute this.

    To require the brackets in order to handle value arrays from (X)HTML forms is at the very least a limitation of PHP. I personally would call it a failing.

Leave a Reply