I imagine this has got to be possible, but I'm not sure where to start...
I'm creating a database-driven menu system for an application here at work. Right now I'm working on some simple administration tools to allow me to configure various settings. One of the things is a list of servers, and some various attributes that can be set either on or off. What I want to do is display this as a table within a form, so that I can check various boxes and save the configuration changes I just made.
I have the table working, queries the database and presents the existing settings just right - one row in the table per database result row, which may be an arbitrary number of rows, depending upon the query that generates the form. I also create unique names for each of the various flags, like FLAG_A_1, where the FLAG_A part corresponds to the particular flag I might want to update, and the _1 reflects that this flag is relative to the result row with a unique identifier of "1".
I might also try to encode the FORM variable names as an array, such as FLAG_A[1], FLAG_A[2], but haven't as yet been able to read the variables in as an array on the form-processing side.
What I can't quite get my head around is how to parse the submitted form - since there may be an arbitrary number of rows in my result, how do I know what variables are being passed when the form is submitted? Is there an elegant way for the form processing script (the ACTION script in my form) to self-discover what variables are available, or do I need to do something hokey, like pass a hidden variable/variables with the array of result row IDs so I can correctly assemble the variables in the receiving script?
Thanks in advance...
Tim
Processing a dynamic form with arbitrary # of FORM variables
Moderator: General Moderators
maybe this (ugly, uncomment, not-explained) code-example helps
Code: Select all
<html>
<body>
<pre><?php print_r($_POST); ?></pre>
<?php
if(isset($_POST['flag']))
{
foreach($_POST['flag'] as $property=>$ids_set)
echo $property, ' has been set for ', implode(',', array_keys($ids_set)), '<br />';
}
// just to have some example data
$dummyFlags = array(
'a' => range(0,4),
'b' => range(0,6),
'c' => range(0,4)
)
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<?php
foreach($dummyFlags as $property=>$ids)
{
echo '<fieldset><legend>', $property, '</legend>';
foreach($ids as $id)
{
$htmlElemId = "flag_{$property}_{$id}";
?>
<input type="checkbox" name="<?php echo "flag[$property][$id]"; ?>" id="<?php echo $htmlElemId; ?>" />
<label for="<?php echo $htmlElemId; ?>"><?php echo $id; ?></label>
<br />
<?php
}
echo '</fieldset>';
}
?>
<p><input type="submit" /></p>
</form>
</body>
</html>