Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving All Form POSTS As Variables

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I've done this before and know I can loop through the returned POSTS to get everything I need using:

Code:
$results = '';

foreach ($_POST as $key => $value) {
    $key = $value;
}

but how can I make return results that I can use as $variable = $value pairs elsewhere in the script? $variable would be the name of the field as submitted by the form. If I try to echo $key[0] to the screen outside the loop, it seems to just give a single character so clearly I've missed something obvious.
 
Strings in PHP can be used as arrays. So doing for instance:

Code:
$var = "This is a string";
echo $var[0]; // "T"

Would simply echo the first character in the string.

Now you are assigning the value of the post variable to the $key variable which you just crated in the for each loop.

At the end, since $key is not an array, you will have overwritten $key as many times as there are values in $_POST, and it will only have the last value assigned in it.

What you probably mean to do, is assign the $value to a key in a new array such as:

Code:
$newarray[$key] = $value;

But that seems like too much work to go through the array using a foreach loop, when you can accomplish the same thing doing simply

$newarray = $_POST;

So $newarray['forminputname'] would have the value from that input.

With that said however you description seems to want to generate a bunch of new variables based on what is in $_POST, so you could do something like:

Code:
echo $forminputame;
And you would get the value from the form.

Personally, I would not suggest you clutter the name space with all those variable names, but if you really want to:

Code:
[red]$[/red][blue]$[/blue]key = $value;

Notice the 2 "$" signs, so the contents of key at the moment is turned into a new variable, and given the value of $value.








----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Perfect! I had already realized the need for the extra $ but I was testing it incorrectly.

Code:
foreach ($_POST as $key => $value) {
    $$key = $value;
}

testing with

Code:
echo $fieldname;

does indeed give the needed value. Thank you!
 
or
Code:
extract($_POST);

but be a bit wary of any code that brings the global variables into scope where they might override other variables. the extract function has protection against this by setting the second argument to EXTR_SKIP. or you can add a prefix to all extractions
Code:
extract($_POST, EXTR_OVERWRITE, "imported_");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top