Jakebert Posted June 25, 2012 Share Posted June 25, 2012 This is how I generate my first form. $results is where I keep the category headers (e.g. President, Vice-President) and $results2 is where the options are for those categories (e.g. Mitt Romney, Barack Obama, Joe Biden, Nameless Faceman, etc.) <?php while($results2 = mysqli_fetch_assoc($sql2)) { ?> <input type="radio" name="<?php echo $results['name'];?>" value="<?php $results2['value'];?>" /> <?php echo $results2['value']; echo "<br />"; ?> My question is: how can I create a process_vote.php form if I don't know how many fields I'm going to get in $POST? based on the link he clicked, he could get one category with just two options, or six categories with 6 options each. Is it easier to send the form back to the same file, or make another file? Quote Link to comment https://forums.phpfreaks.com/topic/264751-passing-dynamic-post-variables-into-another-form/ Share on other sites More sharing options...
g__ink Posted July 11, 2012 Share Posted July 11, 2012 OK, so first of all what you are attempting to do is SO not clear. However, I'm going to make some assumptions and see if I can get this right. So... You have a list of roles and a list of candidates to fill those roles. You are generating a form based on theses roles and candidates. What you want is for the user to pick a single candidate for each role. These roles come from the database, and therefore the number of roles may not be consistent each time you generate the form. So far so good? You are generating your form along these lines: while($results = mysqli_fetch_assoc($sql){ while($results2 = mysqli_fetch_assoc($sq2l){ print('<input type="radio" name="'.$results['name'].'" value="'.$results2['value'].'" />'.$results2['value'].'<br />'); } } which is going to give you something like this: <input type="radio" name="President" value="Mitt Romney" />Mitt Romney<br /> <input type="radio" name="President" value="Barack Obama" />Barack Obama<br /> ... <input type="radio" name="Vice-President" value="Mitt Romney" />Mitt Romney<br /> <input type="radio" name="Vice-President" value="Barack Obama" />Barack Obama<br /> ... Now because you are using radio buttons, when you submit the form you will get 1 option per role (ie 1 for President and 1 for Vice-President) Now as the number of roles may vary, I would suggest using a for loop and a switch statement on the page that you post the data to. ie if(!empty($_POST)){ foreach($_POST as $field => $value){ switch($field){ case 'President': //process the president break; case 'Vice-President': //process the vice-president break; default: //do something else } } } Now this may not work for you, depending on what you want to do with the form data once it is submitted, but hopefully it will give you an idea to play with. PS. This code has NOT been tested so it may not work straight up Quote Link to comment https://forums.phpfreaks.com/topic/264751-passing-dynamic-post-variables-into-another-form/#findComment-1360714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.