I’ve run into a problem that falls under the category of “creating HTML elements from loops of unknown bounds, naming them something iteratively from the loop, and then hoping to refer to those elements later on when you no longer have access to the original bounds of the loop”. But let me explain.
The page I have dynamically generates groupings of radio inputs and a submit button for each such group. The for-loop that creates these assigns names to the radio groups: radio_print-0, radio_print-1, etc. Each radio option within those groups has a value (i.e. the “value=” inside the html tag) created from several variables concatenated together, such that the values could be 1_1_453 or 4_2_24 or… you get it. Each form containing a group of radio buttons has its own submit button, which is likewise named submit-0, submit-1, etc.
Here is the code for how the radio inputs are generated, if it helps. Note that this is 3 levels deep in for-loops, with $i being the outermost loop. All of the radio inputs associated with submit-0 will be named radio_print-0. Also there is some stuff here that is not relevant, like the onclick.
[php]
echo ("<input type=‘radio’ name=‘radio_print-".$i."’ value=’".$i_size."".$i_material."".$gallery[$i][‘imageID’]."’ onclick=‘document.getElementById(“submit-”.$i."").disabled=false’>".$size_names[$i_size]." “.$material_names[$i_material].”: $".$price_list[$i_size][$i_material].".00");
[/php]
Then the code for the submit button generated to go with that group:
[php]
echo (“
Add to Cart”);
[/php]
What I need is for the page after the form is submitted to obtain that radio value (i.e. the 1_1_453, for example). The problem is that I can’t figure out how to check this because I don’t know the string to put in $_POST[] to check for! It could be “radio_print-0”, “radio_print-30”, “radio_print-300”, etc. I don’t know which one has had a value assigned to it. It would be great if there was something like $_POST[].submitted that would return the name of the submit button that was clicked. Then I could extract the number on the end of the string and use that to get the value of the appropriate radio_print. But I don’t think such a thing exists.
I considered putting a hidden input into each form that would have a value equal to the number of forms the page generated, so that I could then run this through a for-loop to check for data, but it runs into the same problem. I would have to give each hidden input a unique name (which would again have to be done iteratively), and the only one that will get submitted is the one in the form with the submit button that is actually clicked. The only thing I can think of doing is having one giant form basically encompassing the entire page. Then I could have a single hidden element in that form with a unique name not dependent on the number of groups of radio/submits, and then I could use that on the page after to run through a for-loop. But god, that seems so horrendous.
I feel like there is probably a very simple solution to this problem but I can’t find it.