开发者

get name of a post variable [duplicate]

开发者 https://www.devze.com 2023-03-29 22:25 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: PHP $_POST 开发者_如何学Pythonprint variable name along with value
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

PHP $_POST 开发者_如何学Pythonprint 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'.


$_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];
}
  • foreach documentation
  • array_keys documentation


Use the extended foreach syntax:

foreach ($_POST as $key => $value)
{
    echo $key . ": ". $value . "\n"; 
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消