Jump to content

how to echo out posted variables that increment by a number


jeff5656

Recommended Posts

I have a form that has variables with a number at the end.

 

When user hits submit, I want to echo them all out in a while loop.  The names of these variables are the same except for the number, i.e. variable1, variable2, etc.  How do I do this?

the number of variables is : $_POST['num_variables']

$i = 0;
while ($i++ < $_POST['num_variables']) {
echo $_POST['variable']  <---- where do I put the $i?  is it $_POST['variable'.$i]?? 

Link to comment
Share on other sites

If there isn't much in the form you could also use a foreach on the $_POST

 

foreach($_POST as $key=>$value)
{
  echo "$key = $value";
}

 

I found that useful when to know when I first learned about looping through the $_POST array

As you stated, it depends on how much information is being sent to the $_POST array, you would most likely get unwanted values that would need to be filtered out, causing more work, however this could be a feasible solution as well

Link to comment
Share on other sites

True.

 

I had a selection of radio buttons 1>5 with tick boxes next to them on a form once.

 

It was easier to add a 'flag' at the beginning of each radio button then when running through each post, if the flag was there I knew it was a radio, then i could check for a checkbox among other possible hidden values dependent on the users action on the page.

 

Then process the data accordingly

 

PS//

It was on here that somebody pointed it out to me and it was a real eye opener at the time.

Link to comment
Share on other sites

True.

 

I had a selection of radio buttons 1>5 with tick boxes next to them on a form once.

 

It was easier to add a 'flag' at the beginning of each radio button then when running through each post, if the flag was there I knew it was a radio, then i could check for a checkbox among other possible hidden values dependent on the users action on the page.

 

Then process the data accordingly

 

PS//

It was on here that somebody pointed it out to me and it was a real eye opener at the time.

 

Why not just do this:

 

<?php

$radios = $_POST['radio'];

echo $radios[0];


?>

<form action="radio.php" method="post" />
<input type="radio" name="radio[]" value="1" />
<input type="radio" name="radio[]" value="2" />
<input type="radio" name="radio[]" value="3" />
<input type="radio" name="radio[]" value="4" />
<input type="radio" name="radio[]" value="5" />

<input type="submit" name="submit" value="submit" />
</form>

Link to comment
Share on other sites

This doesn't work:

 

 

for($i=0;$i<$_POST['num_variables'];$i++)

{

echo $i.": ".$_POST['variable'][$i]."<br>";

}

 

I get 1:

2:

3:

But it's blank.  I want to echo $_POST['variable1'] but instead of 1 I am using $i in the loop, but when i do this:

echo $_POST['variable'] [$i]

it is blank!

Link to comment
Share on other sites

The page displayed several rows

 

On each row started a checkbox then the title of the radio in question then the 5 radio buttons in a line.

 

The user could also add, remove, re-order the radio button rows depending on their settings.

 

So instead of using js to keep track of the order and quantity i simply used a foreach, then search the post array for matching check boxes and hidden fields.

 

The time the php script took to complete the script was so small i didnt think it needed to complicate things further with more js.

 

Saying that, it was a while ago and was an early project so there is do doubt a more efficient way

 

 

Link to comment
Share on other sites

This doesn't work:

 

 

for($i=0;$i<$_POST['num_variables'];$i++)

{

echo $i.": ".$_POST['variable'][$i]."<br>";

}

 

I get 1:

2:

3:

But it's blank.  I want to echo $_POST['variable1'] but instead of 1 I am using $i in the loop, but when i do this:

echo $_POST['variable'] [$i]

it is blank!

 

Okay, now I see what you're trying to do. Try this then:

 

for($i=1;$i<=$_POST['num_variables'];$i++)
{
echo $_POST['variable' . $i] . '<br />';
}

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.