Page 1 of 1

Passing an array and the displaying as hidden field.

Posted: Tue May 14, 2002 7:48 am
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.

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

Posted: Tue May 14, 2002 9:17 am
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";

Posted: Tue May 14, 2002 1:59 pm
by volka
which ist the same as
foreach ($HTTP_POST_VARS as $key=>value)
....