I am creating a form based on a user selection of $node_number … so the form looks like:
function createForm($node_number) {
echo "<form id=\"form\" name=\"form\" action=\"molecule_display.php\" method=\"post\">";
for ($n = 1; $n <= $node_number; $n++) {
echo "<fieldset class=\"step\">
<input id=\"node_title" . $n . "\" name=\"node_title" . $n . "\" />
<input id=\"node_comment" . $n . "\" name=\"node_comment" . $n . "\" type=\"textarea\" />
</fieldset>";
}
echo "<input type=\"hidden\" name=\"node_number\" value=\"" . $node_number . "\">
<button id=\"submit_node\" type=\"submit\">Submit</button>"
echo "</form>";
}
Which will create $node_number of versions of that form element. My question is how to dynamically name the form elements to be able to manage them easier when I am processing them. The way I’m doing it right now, by adding the $n iterator to the name attribute is not ideal I think.
I understand that I can declare the name=”” attribute as an array like name[]=”” … in terms of giving each sub-element of the larger form a unique name.
I’m guessing I want a multi-dimensional array of the individual form segment … just not sure how to best handle those within a form and within the $_POST variable.
Does anyone have any suggestions?
I think you can do it this way:
And then get
$_POST['nodes']which will be multidimensional array, which you can iterate withforeach. You will get$_POST['nodes'][1] = array('node_title'=>... , 'node_comment'=>...);and so on.