Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Over the past year I have found your site to be EXCELLENT. Never have I been able to find so many answers to such vast problems and it is an excellent service..."

Geography

Where in the world do Tek-Tips members come from?

Retrieving All Form POSTS As Variables

PCHomepage (Programmer)
24 Jul 12 16:20
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.
vacunita (Programmer)
24 Jul 12 17:32
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

$$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

PCHomepage (Programmer)
24 Jul 12 18:23
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!
jpadie (TechnicalUser)
25 Jul 12 11:15
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_"); 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close