954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

$_POST and arrays

I need to load a value from a recently posted page...

[php]for ($i=0; $i<10; $i++)
{
$_SESSION['someVar'][$i] = $_POST['someVar'][$i]
$_SESSION['anotherVar'][$i] = $_POST['anotherVar'][$i]
$_SESSION['yetAnotherVar'][$i] = $_POST['yetAnotherVar'][$i]
}[/php]

now the session part works just fine, but the $_POST part just blows up on me! How can I fix this? I want to keep the $_POST there, because I know I can just do $someVar[$i] and that will work but it's not what I want to do. I have seen a foreach loop and thought about that. But, would that keep all the variables tied together with the same index? I really dont want to use a foreach loop if I dont have to, but if thats what I need to use, then so be it.

Thanks!

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 
[php]$_SESSION['someVar'][$i] = $_POST['someVar'][$i][/php]


$_POST['someVar'][$i] is not valid unless your 'someVar' FORM element is an array of checkboxes or you have multiple text fields with the same name. Normally, it would just be $_POST['someVar'].

Whether it is an array or not, you can just do this:
[PHP]
$_SESSION['someVar'] = $_POST['someVar'];
[/PHP]It does not matter whether 'someVar' is an array or not--the session var will still receive the value.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

How about this?[PHP] $_SESSION['someVar'][$i] = $_POST['someVar'.$i] [/PHP]In this case, your form variables are someVar1, someVar2,...

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

why not using define function!?

val542
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

val542, your reply makes no sense. The define() function does not even apply to this scenario.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You