Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the key values from $_POST?

Tags:

echo $_POST["name"]; //returns the value a user typed into the "name" field

I would like to be able to also return the text of the key. In this example, I want to return the text "name". Can I do this?

like image 393
Haabda Avatar asked Oct 08 '08 17:10

Haabda


People also ask

How do I get keys in PHP?

PHP: array_keys() function The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

How do you access a key in an array?

If you want to access the key of an array in a foreach loop, you use the following syntax: foreach ($array as $key => $value) { ... }

What does $_ POST do in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".

Is $_ POST an array?

The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.


2 Answers

$_POST is just a normal associative array so you can also loop over the entire thing like this:

foreach($_POST as $key=>$value)
{
  echo "$key=$value";
}
like image 116
Mark Biek Avatar answered Nov 02 '22 06:11

Mark Biek


Check out the array_keys() function assuming this is PHP.

http://us2.php.net/array_keys

like image 43
theraccoonbear Avatar answered Nov 02 '22 06:11

theraccoonbear