POST- Passing to a variable on the other side

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
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

POST- Passing to a variable on the other side

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
?>
Last edited by volka on Sun May 11, 2003 8:52 pm, edited 1 time in total.
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Post 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
Post Reply