Passing Input Arrays in PHP

Posted by Jetlogs @ 12:50 am
Category: PHP, Web Development

It seems that many people have been stumbling on my website also trying to find out how to pass input arrays in just plain PHP. For this tutorial, all you need to have is the basic knowledge of using PHP. I won’t deal with form / input validations too much, but here is how to do it in short and simple steps:

The first step we need to do is create our form which will pass a multi-valued parameter. For example, a checkbox list or a multiple-select list. For our example, we shall use a checkbox list. Here is what it would look like:

Which text editor do you use?<br />
	<form method="post" action="process_input.php">
	  <input type="checkbox" name="editor[]" value="Notepad" />
	  Notepad<br />
	  <input type="checkbox" name="editor[]" value="Scite" />
	  Scite<br />
	  <input type="checkbox" name="editor[]" value="Crimson Editor" />
	  Crimson Editor<br />
	  <input type="checkbox" name="editor[]" value="Dreamweaver" />
	  Dreamweaver<br />
	  <input type="checkbox" name="editor[]" value="vim" />
	  vim<br />
	  <input type="submit" value="submit" />
	</form>

There are basically 3 simple rules in passing input arrays from forms:
1. All inputs should be inside a valid <form> element
2. All members of the same input array should have the same input name.
3. The input names should have an opening and closing bracket([]) to denote that the input being passed is an array

Now that we have passed the form as an input array, how do we process it in PHP? Depending on the method used on the form (POST or GET), we can use one of three predefined variables to extract the value of these inputs. And they are:

1. $_POST - variable used to extract the values of inputs passed through the POST method
2. $_GET - variable used to extract the values of inputs passed through the GET method
3. $_REQUEST - variable used to extract the values of inputs passed either through the POST, GET, or cookies. This is not recommended for serious use due to security implications

In our case, since we have used the POST method, we need to access the $_POST variable to retrieve the data we sent from the form. In our example, $_POST["editor"] will contain the array of values that have been selected from the form. We could then simply use a foreach statement to find all the values that were selected. Here is how the code will look like without any form validation being done:

foreach($_POST["editor"] as $editor)
	{
		echo $editor . "";
	}

However, with a few validations for checking NULL values, here is what the code would end up like:

if (isset($_POST["editor"]) && is_array($_POST["editor"])
&& count($_POST["editor"]) > 0)
{
	foreach ($_POST["editor"] as $editor)
		{
			echo htmlspecialchars($editor, ENT_QUOTES);
			echo '<br />';
		}
}

Special thanks to Tim for the suggestion.

PHP Input Arrays Demo
Download Demo Source Code


3 Comments »

3 Comments to “Passing Input Arrays in PHP”

  1. tim
    1

    Thank you for that tutorial. It would be better to check $_POST['editor'] with isset and then to use is_array().

    if (isset($_POST['editor']) && is_array($_POST['editor'])
    {
    if (count($_POST['editor']) > 0)
    {
    //do something
    }
    }
    else
    {
    echo ‘Required parameter missing or wrong type.’;
    }

    Best regards,
    Tim

  2. tim
    2

    Unfortunately the formatting got lost…

    I’ve uploaded the code to pastebin. Here’s the address:
    http://pastebin.ca/626091

    Best regards,
    Tim

  3. Jetlogs
    3
    Author Comment

    Thanks a lot Tim. I think that is a much better way of checking whether POST array is of the proper type and check if its not empty. Let me update the article. Thanks :)

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License