Jump to content

passing dynamic $POST variables into another form?


Jakebert

Recommended Posts

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?

Link to comment
Share on other sites

  • 3 weeks later...

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.