At work, I was tasked to create a multidimensional form. For example. The user was going to define X number of animals, and each animal was to have X number of syndromes, and each syndrome for each animal could have X number of lesions.
So in programming you can set this up as a multidimensional array. Say you were interested in the name of the 2nd lesion of the 1st syndrome of the 23rd animal, $animal[22][syndrome][0][lesion][1][name]. Pretty simple. But, how the hell do we implement that into an HTML form (without JavaScript)?
I learned that since PHP 4, there is now support for multidimensional forms. Like so:
<pre> <?php print_r($_POST); ?> </pre> <form method="post" name="main" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="field[0][name]" value="zero" /> <blockquote> <input type="text" name="field[0][sub][0]" value="zeroSub0" /> <input type="text" name="field[0][sub][1]" value="zeroSub1" /> </blockquote> <br /> <input type="text" name="field[1][name]" value="one" /> <blockquote> <input type="text" name="field[1][sub][0]" value="oneSub0" /> <input type="text" name="field[1][sub][1]" value="oneSub1" /> </blockquote> <br /> <input type="submit" name="submit" value="submit" /> </form>
To extend this further, you can just code inside a loop as follows:
<input type="text" name="field[<?php echo $fieldID; ?>][sub][<php echo $subID; ?>] />
when posted this can be accessed as an array in the $_POST array:
$_POST['field'][$fieldID]['sub'][$subID]
or
$_POST['field'][22]['sub'][0]
No string parsing or explode()ing necessary.