views:

267

answers:

9

Hey, I'm programming a feedback form on a website for a client, however, there are over 100 inputs (all uniquely named). I was wondering if there was a loop I could run to get all of the variables, do you have to call them like this:

$variable = $_REQUEST['variable'];

EDIT:

I'm going with $_POST as recommended by everyone here - thanks for the input! I'll just manually go through and write a line for each $_POST I have.

A: 

The $_GET and $_POST array variables contain every parameter passed via URL or POST method respectively. You should rather use that variables instead of extracting every item like the register_globals option or extract function does. See Using Register Globals for the reasons.

Gumbo
You might want to clarify that register globals is *NEVER* a good idea. For *any* reason. People who *think* its a good idea end up writing security holes under the guise of a website.
Kent Fredric
A: 

As in my comment above, don't use REQUEST, use POST. I'd probably write a line for each one so that I was aware of what was happening. You don't want people to post extra variables that still get parsed.

Rich Bradshaw
A: 

You can write simple code to algorithmically obtain all inputs from a posted form, but this is a dangerous business, one that is ripe for exploitation.

byte
A: 
echo "<pre>";
print_r($_POST); // or $_GET
echo "</pre>";

If you are dealing with items that you possibly won't know the name of but need the values then you are dealing with a much larger problem than you think.

But the above snippet should show you all variables set and their values when you submit the form

Ólafur Waage
Useful to know for the OP, but won't help you actually do anything with them.
Rich Bradshaw
+2  A: 

You can loop over all veriables in the $_PREQUEST array:

foreach($_REQUEST as $key=>$value){
  //doStuff
}

However this will include all send parameters, not only the input. Also you should not use $_REQUEST but $_POST or $_GET

Pim Jager
+2  A: 

If you don't want to have hundreds of uniquely named variables and want to have arrays of data turn up client side, there is a handy form trick you may want to try.

<form>
  <input name="foo[a]" type="text" />
  <input name="foo[b]" type="text" />
  <input name="bar[]" type="text"  />
  <input name="bar[]" type="text"  />

Client Side:

<?php
   $_POST['foo']['a']; 
   $_POST['foo']['b'];
   $_POST['bar'][0];
   $_POST['bar'][1];
?>
Kent Fredric
+1  A: 

I think you should start out with a list of all variables that you expect, and loop only over those. If you don't, hackers can inject any variable name... In fact, you're reemplementing the old, terribly bad idea of Register Globals. So, don't do that...

In fact, why not keep the input in an associative array, just like $_POST? You might still want to remove those values that you didn't expect.

bart
A: 

I can't add a comment on Pim Jager's post, but here's an example code:

foreach($_POST as $key=>$value) {
    echo($key.' > '.$value.'<br />');
}
Sherman
A: 

Use an array with the fields you want copied from POST and store the data in an array. It's a lot easier to maintain.

$fields = array("name","email","addr","bla","bla2");
$form = array();
foreach($fields as $field){
    $form[$field] = $_POST[$field];
}

Now you've got all the data inside $form.
You could also make a similar thing when creating the form. (Although it's trickier..)

Cheers!

0scar