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]?? 

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

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

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.

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>

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!

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

 

 

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 />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.