On Tuesday, March 26, 2002, at 01:18  PM, Jeff Hatcher wrote:

> I have a form that gets repeated depending on number of members in a
> group(1 form surrounds all members). I separate the entries by assigning
> a count value to the names of the inputs (Ex. <input type=text
> name=address$count value=>). Does anyone know how I can pull the values
> back out of the $_POST[]?
>
> Example of ideal scenario that does not work:
>       case "process1":
>               for ($i=0;$i<$_POST[count];$i++)
>               {
>                       $_POST[address$i]       
>               }

The initial form:

for ($i = 1; $i <= $_POST['count']; $i++) {
   print "<input name=\"address{$i}\" type=\"text\" />\n";
}
print "<input name=\"count\" type=\"hidden\" value=\"" . 
$_POST['count'] . "\" />

What the above loop does is it makes a number of address inputs equal to 
$count... if $count is equal to 3, then you will get

<input name="address1" type="text" />
<input name="address2" type="text" />
<input name="address3" type="text" />
<input name="count" type="hidden" value="3" />

I changed your $i from 0 to 1 because it makes it mentally easier to 
work with (for me at least).

Now in your next script, which is the target of the form, here is what 
you want to have:

for ($i = 1; $i <= $_POST['count']; $i++) {
   // do some code with $_POST["address{$i}"], such as
   // entering it into a database or echoing it to the user in HTML
}

Some further notes -- the hidden form field was so that you could give 
the second script the "count" variable, so it knows how many loops to do 
(this value is otherwise not available to the second script).  Also, the 
use of doublequotes in the second script ($_POST["address{$i}"]) is 
imperative because with singlequotes the variable $i will not expand to 
$_POST["address2"] or whatever.

HTH,

Erik





----

Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to