<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Submitting a multidimensional array from a form to PHP via POST

Published on
31,811 Points
25,811 Views
Last Modified:
Approved

After much searching on the subject myself, I've found that many people have wondered whether it's possible to submit a multidimensional array from an HTML form to a PHP script.  And for some reason, all the articles answering this query are convoluted or wrong.  The answer is simple: YES!

It is not only possible, but very straight-forward to submit a multi-dimensional array from a form to a PHP script.  The functionality was added in PHP 4 (and nobody but... well nobody... should still be using a version of PHP prior to 4).   Here is how it works:

Let's say you want to let someone pick several "options" to make available for a new product on your web page, so you have some check boxes for colors, sizes and types.  But you have lots of other data in your form you don't want to get it confused with (perhaps there's another "type" of something else also in your form), so you want to contain all of these selections in a sub array called "options" so you can find them easily once you get to the PHP.  Here's how that would look in XHTML:

<form action="process.php" method="post">
...
check which options this product will be available in:
SIZES:
<input type="checkbox" name="options[sizes][]" value="small" />small
<input type="checkbox" name="options[sizes][]" value="medium" />medium
<input type="checkbox" name="options[sizes][]" value="medium" />large

COLORS:
<input type="checkbox" name="options[colors][]" value="red" />red
<input type="checkbox" name="options[colors][]" value="blue" />blue
<input type="checkbox" name="options[colors][]" value="green" />green
<input type="checkbox" name="options[colors][]" value="orange" />orange

TYPES
<input type="checkbox" name="options[types][]" value="floppy" />floppy
<input type="checkbox" name="options[types][]" value="compact" />compact
<input type="checkbox" name="options[types][]" value="hard" />hard
<input type="checkbox" name="options[types][]" value="digital" />digital
<input type="checkbox" name="options[types][]" value="analog" />analog

...
</form>

Open in new window


And as long as you're running a version of PHP that is not ancient, your $_POST array will look something like this (depending on options checked).:

Array
(
    ...
    [options] => Array
        (
            [sizes] => Array
                (
                    [0] => small
                    [1] => large
                )
            [colors] => Array
                (
                    [0] => red
                    [1] => green
                )
            [types] => Array
                (
                    [0] => floppy
                    [1] => analog
                    [2] => digital
                )
        )
    ...
)

Open in new window


Pretty much how you'd expect it to look.  Nothin' tricky about it!  So don't worry, go ahead and use them; they work!

0
Comment
Author:mltsy
1 Comment

Expert Comment

by:Qadzealous Abdulqadir
Assuming I want to echo these results individually how would I go about it. I have tried different methods none works efficiently
0

Featured Post

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Join & Write a Comment

The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month