LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2007, 08:18 AM   #1
DMeerholz
LQ Newbie
 
Registered: Jun 2007
Posts: 22

Rep: Reputation: 15
Load PHP posted vars into array


Hi,

------------
EDIT

Sorry, I just read what I typed in the heading... tried to edit it but I cant...

Load HTML posted variables into PHP variables ... makes a bit more sense.

------------

I am trying to write a function that will load vars posted from a web page into PHP vars with the SAME name.

Like this:

<form>
<input name="xxx" type="text" value="111">
<input name="yyy" type="text" value="222">
<input name="zzz" type="text" value="333">
</form>

When this is posted to my php, I want to call a function

LoadVars();

This will create the vars $xxx, $yyy and $zzz and set them to the correct values.

The code below gives a list of all html vars and values, but I am struggling to do what I mentioned above.

<?php
foreach($_POST as $key=>$val) {
echo '$_POST['.$key."] = $val<br>n";
}
?>

Any ideas???

Thanks.

Last edited by DMeerholz; 07-03-2007 at 08:21 AM.
 
Old 07-03-2007, 10:02 AM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
PHP Code:
<?
    
foreach($_POST as $key=>$value)
    {
        $
$key=$value;
    }
?>
What I don't like with this approach, is that it gives no control whatsoever. So I suggest you change the names of the inputs OR check something in the foreach, for instance, the fact that you named your variables with 3 letters, gave me an idea..

PHP Code:
<?
    
foreach($_POST as $key=>$value)
    {
        if(
strlen($key)!=3)
        {
            continue;
        }
        $
$key=$value;
    }
?>
Edit: also check: http://www.php.net/manual/en/functio...-variables.php
And http://www.php.net/manual/en/function.extract.php
Cheers.

Last edited by nc3b; 07-03-2007 at 10:06 AM.
 
Old 07-05-2007, 01:15 AM   #3
DMeerholz
LQ Newbie
 
Registered: Jun 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Thanks nc3b, the is exactly what I wanted.

As for the control with 3 letter vars, the example I gave was just something I typed quick, they wont really be like that, but you are right, will probably add a prefix to all vars and check them that way.
 
Old 07-05-2007, 01:46 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What I do is to store the variables I'm expecting as a session variable array, and I then loop through the POST variable checking to see if they are in the list of expected variables any new ones are ignored (an error can be logged if you wish)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't use my PHP array. rblampain Programming 4 06-23-2006 07:19 AM
How to receive arbitrary HTTP posted XML data in a php script rstewart Programming 2 03-10-2006 11:36 AM
PHP array help? HappyDude Programming 1 10-13-2003 06:48 PM
Uptime posted to html via PHP? bkeating Linux - Software 5 06-20-2002 03:51 AM
php array gui10 Programming 2 03-26-2002 02:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration