Extracting $_POST data from page with unknown number of radio & submit groups

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.

HUH??? $_POST[] always returns the name of posted submit buttons! (And, also it’s value!)

So,

When ONE of these is pressed the value of $pressed=$_POST[“MyButton”]; will equal 1 or 2…

Note: Only one SUBMIT button can be pressed at a time in any HTML FORM. So, you can play with the values
and the names as you need. If you have one button that is special call it a unique name. IF not, set it’s value
to something that makes sense… Like 1,2,3,1_1_453, 4_2_24…

Also, if you need to, you can look at all of the values in $_POST[] as it IS just an array…

Not sure if that helps, but, I hope it does…

Wait, can you use the same name for different Submit buttons even if they are in different tags? Anyway, that wouldn’t work because the value of the Submit button is the text it displays in the button, and I need the text displayed to be “Add to Cart”. But, I think I haven’t explained it well enough. Let me make up an example that simplifies the irrelevant parts :
[php]
for ($i=0;$i<$n;i$++) //$n could be anything
{
echo ("");
echo ("

    ");
    for ($j=0;$j<$k;j++) //$k could be anything
    {
    echo ("<input type=‘radio’ name=‘radio_".$i."_".$j."’ value=’".$data[$i][‘innerdata’][$j]."’>");
    }
    echo ("
"
echo ("");
echo ("</form");
}
[/php]
So what you have are multiple forms of an unknown quantity, each of which contains a list of radio options of an unknown quantity and a submit button unique to its parent form. Because of the unknowns, the names of the radio options and submit buttons cannot be static; they have to be named iteratively based on the variable used in the for-loop they are generated by. And because the termination of both for-loops is an unknown value, the next page cannot simply iterate through identical loops, because it won’t know when to terminate the loop.

Well, I am guessing you do not understand HTML forms. ANYTHING can be a submit button. In several sites
that I maintain, I use this type of code for a button…

This is the "value" that is show to the user...

You can put anything you wish in there… I create them dynamically using PHP code as needed.
I quite often use a fancy name for them. You can place 100 buttons on a form. ( As long as they are of type
“submit” ! ) And, when I need to check for them, I just do a check in the $_POST[] array for anything that
starts with “Somebutton” and then, take the “1” or whatever number out of the name and use it in my
computations. I use it a lot for special or odd buttons, like “DELETE123”, “UPDATE123”, “EDIT123” or any
thing I can think of if needed. A lot of time, I will show a long list of rows of data with special buttons that
pass data to calculation routines.

As you now see, a button can be very versatile… Just think out what you really need to do and create your
own button… Let’s take your description… Well, you can have as many forms as you need on a page.
The buttons can be encoded using the form name or other special coding. Or, you can just use one scheme.
Any name you want to make up. It could be “Form1_Section2_Button99” or anything you can dream up to
fit your scheme. TO decode that, you can use a simple EXPLODE using the underscore to separate them.

In programming there is always a dozen ways to sort out an issue. This is a common one.

Now, your radio buttons… Hmmmm… You do NOT use radio button with separate names. For that, you
use checkboxes. Radio buttons are used to create a “pick one from many” options. The name is always
the same with separate values. You read one name and get the value for a field. For multiple named items
you can use checkboxes.

If you mean you have a list of radio button groups in many forms, you give each radio “GROUP” a name and
the user picks one from each group. That would be done with a name that has the “GROUP” name, not
individual values. Since the value is the answer they select, you already know the “VALUE” of the one
group of radio buttons. So, something like this…



So, you read the two ( 2 ) radio buttons and you have the two values the user selected. You do not name
all of these radio buttons eight different names as you would have to make a complicated way of decoding
the names before you can get the values. Did that make sense?

Hope this info helps… Good luck!

And because the termination of both for-loops is an unknown value, the next page cannot simply iterate through identical loops, because it won't know when to terminate the loop.

Of course you can. You just need to set up the naming convention of the radio buttons so it can know how
to find them. You can compare for the first part of all the names posted and if they start with your code such
as “Radio_” then, your code can grab the rest of the name and deal with it. You just need to do a FOREACH
pass thru the $_POST[] array. Remember the $_POST[] values are just an array and you can loop thru it
with ease. Then, you just do your compares and handle the data. Perhaps you are over thinking this.

Yeah, not sure why I forgot about that…

I just do a check in the $_POST[] array for anything that starts with "Somebutton"
This is what I have been trying to figure out how to do. My previous use of $_POST has been doing things like if (isset($_POST['something']), but it should be obvious that this method won't work if I don't know what the 'something' is. You're talking about searching the $_POST[] array and detecting what's there. I know it sounds dumb but I can't seem to figure out how to go about doing that. You mentioned using a foreach later... would you mind giving a sample code of how you would structure this? I assume it starts like this: [php] foreach($_POST as $test) { //but I'm not sure what to do in here } [/php] I want to look for $_POST['radio_*'] where the * is some number. And I'm guessing there is a way to use substrings here for that.
Now, your radio buttons... Hmmmm.... You do NOT use radio button with separate names. For that, you use checkboxes. Radio buttons are used to create a "pick one from many" options. The name is always the same with separate values. You read one name and get the value for a field. For multiple named items you can use checkboxes.

If you mean you have a list of radio button groups in many forms, you give each radio “GROUP” a name and
the user picks one from each group. That would be done with a name that has the “GROUP” name, not
individual values. Since the value is the answer they select, you already know the “VALUE” of the one
group of radio buttons. So, something like this…



Yeah, I knew I was going to make a mistake typing up the example. What you have here is what I meant to do. The name should have only iterated for $i, not for both $i and j$.

I think I have been. I was thinking I could basically only get $_POST data by checking isset and then reading it if it was, but going through with a foreach seems like it would solve the problem, although I could still use a tiny bit of hand-holding filling in that code above.

Based on how this is progressing, the design needs work. If you are working on “adding to cart”, you are making this WAY to complicated.

Yes, Astonecipher! He did not tell us what he was trying to do. Therefore, I was just helping answering his
questions… Not sure if the design is sound, but, giving him ideas to create a solution with…

Technetium, You must remember that the $_POST[] array is just an array…

foreach($_POST as $key=>$value) {
If $key=“something” do something with $value
}

Yes, that’s what I needed! I had completely forgotten about that way of doing foreach loops. It’s been a while since I did this stuff. Thanks a lot!

Basically, the page is an image gallery where each thumbnail opens a Bootstrap modal containing a large version of the image followed by description and options to buy prints. Each modal contains a form with radio options and a submit. It is impossible to hardcode how many images are in the image gallery, so this is why the number of forms is unknown. The print options are also unknown because not every print option is available for every image.

I’m sure it could be done differently, this is just the method that came to me while I was working on it.

Well, glad you solved it. As I always say, it’s a nice warm fuzzy feeling to solve a programming puzzle!

CYA in the Bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service