5

Possible Duplicate:
PHP $_POST print variable name along with value

I have a form (whatever number of fields).. this form will send $_POST data.. I want to return every $_POST variable value and name.

I wanna do soemthing like this :

foreach($_POST as $field){
    //****some code*****//
}

in a way that will display the fields and values like this:

name : Simo Taqi
email : example@ymail.com

in other words :

if I have a post variable : $_POST['city']='las vegas'

I want to know how to get the name of the variable : 'city'.

Community
  • 1
  • 1
SmootQ
  • 2,096
  • 7
  • 33
  • 58

3 Answers3

19

$_POST is populated as an associative array, so you can just do this:

foreach ($_POST as $name => $val)
{
     echo htmlspecialchars($name . ': ' . $val) . "\n";
}

Additionally, if you're just interested in the field names, you can use array_keys($_POST); to return an array of all the keys used in $_POST. You can use those keys to reference values in $_POST.

foreach (array_keys($_POST) as $field)
{
    echo $_POST[$field];
}
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • 4
    I would escape those values before printing them, if this is in an HTML page: `echo htmlspecialchars("{$name}: {$val}\n");` – Arnaud Le Blanc Aug 23 '11 at 15:52
  • You're actually recommending encapsulating variables inside double quotes for literally no reason? – AlienWebguy Aug 23 '11 at 15:56
  • 1
    @AlienWebguy If you don't want to, then don't. – Jimmy Sawczuk Aug 23 '11 at 15:58
  • 1
    If you understood core PHP and followed best practices, you wouldn't. – AlienWebguy Aug 23 '11 at 16:00
  • @AlienWebguy Fine, it's changed. Not that it made any significant difference at all, in the scope of this example. – Jimmy Sawczuk Aug 23 '11 at 16:05
  • Other than you just encouraged some bad coding habits on a guy who's learning, you're right. Good for you bro. – AlienWebguy Aug 23 '11 at 16:09
  • The core of the question is this: "I want to know how to get the name of the variable : 'city'." That's what I was meaning to answer. I'm not disputing that interpolating hurts performance, but it's not something I'm particularly worried about or feel like arguing about for a three-second example. – Jimmy Sawczuk Aug 23 '11 at 16:16
  • can i use this method for radio button groups? As you know for radio buttons we need to post only selected radio button? – Furkan Nov 25 '15 at 13:27
  • This answer (and all other answers until now) fail if the POST data contains a value like `something[abc]=123`, because then `$val` will not be a string, but an array `["abc" => 123]`. `array_keys` will only contain `something` and not `something[abc]`. – MaPePeR Jun 27 '22 at 07:51
7

Use the extended foreach syntax:

foreach ($_POST as $key => $value)
{
    echo $key . ": ". $value . "\n"; 
}
aorcsik
  • 15,271
  • 5
  • 39
  • 49
0

I disagree with this post, since it assumes that output is always intended for a browser. One should not get into the habit of this. \n is the correct usage and can be easily converted before output ( to a browser ) using the nl2br() function.