Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

PHP will organize form values into arrays if you ask nicely (See related posts)

I took stole this example directly from php.net but it is worth the infraction. Name your form fields correctly and they are placed in arrays in the $_GET or $_POST arrays. This example would return something like this if you selected two of the "beer" elements.

Array
(
[personal] => Array
(
[name] => Fred Derf
[email] => fred@example.com
)

[beer] => Array
(
[0] => warthog
[1] => guinness
)

)

<?php
if ($_POST) {
    echo '<pre>';
    echo htmlspecialchars(print_r($_POST, true));
    echo '</pre>';
}
?>
<form action="" method="post">
    Name:  <input type="text" name="personal[name]" /><br />
    Email: <input type="text" name="personal[email]" /><br />
    Beer: <br />
    <select multiple name="beer[]">
        <option value="warthog">Warthog</option>
        <option value="guinness">Guinness</option>
        <option value="stuttgarter">Stuttgarter Schwabenbräu</option>
    </select><br />
    <input type="submit" value="submit me!" />
</form>

You need to create an account or log in to post comments to this site.