Foreach grabbing post data

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
RobbieL
Forum Commoner
Posts: 31
Joined: Fri Mar 23, 2007 5:57 pm

Foreach grabbing post data

Post by RobbieL »

I've been pulling my hair out over this all afternoon, and it's driving me nuts. At work a few months ago, I was using a foreach loop that would grab all the post data from a form. After this foreach, I was able to access any post data from the form just using something like $form['username'];, where username is the name of an input field in the form. So if we wanted to input the username into a database, we'd just stick $form['username'] in the INSERT and it'd work a treat.

I've been trying to recreate this today, and have gotten no where. I'm starting to think it was some sort of black magic the guys had going on down at the office! :wink:

Anyone able to shed some light on this and stop me from going bald? Cheers.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Foreach grabbing post data

Post by Christopher »

What code have you tried? The post data is in the superglobal array $_POST.
(#10850)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Foreach grabbing post data

Post by JAM »

you mean something similiar to this?

Code: Select all

<form method="post">
    <input type="submit" name="foo" value="bar" />
</form>
<?php
    if (!empty($_POST['foo'])) {
        echo $sql = 'insert into table (\'col\') VALUES(\''.$_POST['foo'].'\') where moo = \''.$_POST['foo'].'\'';
        echo '<br />';
        echo $sql = "insert into table ('col') VALUES('{$_POST[foo]}') where moo = '{$_POST[foo]}'";
    }
?>
Just examples, no consideration about verification nor security taken...
RobbieL
Forum Commoner
Posts: 31
Joined: Fri Mar 23, 2007 5:57 pm

Re: Foreach grabbing post data

Post by RobbieL »

Cheers for the respones guys. What I have so far, which when echo'd just seems to return a random letter, is:

Code: Select all

 
foreach ($_POST as $form=>$value)
{
         $form['$value'];
}
 
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Foreach grabbing post data

Post by JAM »

You could use $_POST directly (as oposed to your $form), but you mean this?

Code: Select all

   foreach($_POST as $k => $v) {
        $form[$k] = $v;
   }
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Foreach grabbing post data

Post by pickle »

There's no need to just transfer raw $_POST data into another array - that's a complete re-invention of the wheel. However, you should absolutely be massaging the data (as in, removing and adding slashes where necessary). In that case, you should store the massaged data in $form.

~JAM's code make's sense.

Your code doesn't because:
  1. when you put $value in single quotes, PHP interprets it as the dollar sign followed by the string "value". You probably meant to put it in double quotes.
  2. I think you're confused as to how foreach works. In every iteration of your foreach() loop, you're not setting anything, just declaring an array element. Use ~JAM's code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RobbieL
Forum Commoner
Posts: 31
Joined: Fri Mar 23, 2007 5:57 pm

Re: Foreach grabbing post data

Post by RobbieL »

Big apologies for the late reply, but I finally managed to grab a spare moment and give JAM's example out, and of course it worked a treat! :)

Thanks again. Very much appreciate it, :wink:
Post Reply