Passing an array and the displaying as hidden field.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sillywilly
Forum Newbie
Posts: 19
Joined: Thu May 02, 2002 5:11 pm

Passing an array and the displaying as hidden field.

Post by sillywilly »

Currently i have:

<?php
foreach($HTTP_POST_VARS['S1Q3'] as $value) {
echo "<input type=hidden name=\"S1Q3\" value=\"$value\">\n";

}
?>

I would like is so that the value would be the values of the items in the array, thus:

Code: Select all

<input type="checkbox" name="S1Q3&#1111;0]" value="0"><br>
Playing interactive games is unsociable
<input type="checkbox" name="S1Q3&#1111;1]" value="1"><br>
Playing interactive games is boring
<input type="checkbox" name="S1Q3&#1111;2]" value="2"><br>
Playing interactive games is geeky
<input type="checkbox" name="S1Q3&#1111;3]" value="3"><br>
They're expensive
<input type="checkbox" name="S1Q3&#1111;4]" value="4"><br>
I have never had the opportunity
<input type="checkbox" name="S1Q3&#1111;5]" value="5"><br>
I don't have the time
<input type="checkbox" name="S1Q3&#1111;6]" value="6"><br>
Other
<input type="checkbox" name="S1Q3&#1111;7]" value="7"><br>
So that the hidden field in the next form would be:

<input type="hidden" name="S1Q3" value="0|1|3"> and so on....

Any ideas.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

RE: Passing an array and the displaying as hidden field.

Post by Johnm »

Here is an example of how I have done that, I am sure that there are better ways but this has worked for me.

Code: Select all

if ( is_array($HTTP_POST_VARS)) 
        reset($HTTP_POST_VARS);

    while (list($key, $val) = each($HTTP_POST_VARS))
    &#123;
         $name=strtoupper($key);
         $val=strtoupper($val);
          echo "<input type="hidden\ name="$name" value="$val">\n";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

which ist the same as
foreach ($HTTP_POST_VARS as $key=>value)
....
Post Reply