Page 1 of 1

POST- Passing to a variable on the other side

Posted: Sun May 11, 2003 8:42 pm
by Templeton Peck
My dilemma:

Once the form gets submitted, and comes through to the target php file, I have this variable problem.. the values being passed from the form are called name1, name2, name3 and so on.. a bunch..

is there a way to reference them other then typing out name1, name2 etc.

ie. something like $name = $_POST['name']; where I can just use loops to assign the variables.. the example I gave on the line above doesn't work for me. Where i is 1,2,3,4,5 and so on from a loop counter


$name = $_POST['name'];
$name1 = $_POST['name2'];
$name2 = $_POST['name3'];

is what i've been doing, but I can't do it because there's too many, a loop would work if I could reference inside the brackets with a counter like "i"

any help would be appreciated

Posted: Sun May 11, 2003 8:49 pm
by volka
it doesn't matter how the key has been created, php takes the argument to whatever the expression evaluates.

Code: Select all

<?php
$array['name'] = 1; // a string literal passed, php is looking for a key that equals to that string
$key = 'name1';
$array[$key] = 1; // same procedure, this time not a literal but a variable holding a string
$key = 'name' . '1';
$array[$key] = 1; // key came into existence in a different way (slightly), but exactly the same lookup
$array['name'.'1'] = 1; // can be done within [ ], too

for ($i=0; $i!=10; $i++)
	$array['name'.$i] = 1; // that's probably, what you want.
?>

Posted: Sun May 11, 2003 8:51 pm
by Templeton Peck
I can't tell you how much I appreciate you helping me :D

works like a charm, thanks so much.. I tried something similar but was just off