Get Name of an input in php

0

Is it possible to obtain the name of an input that was sent by post through php?

Example:

<form method="post" action="controller.php">
<input type="text" name="nombre" value="perro">
<input type="submit" value="enviar">
</form>

Thanks in advance

    
asked by Pablo Moraga 13.12.2017 в 22:11
source

2 answers

4

Since through $_POST what we get is:

  

An associative array of variables passed to the current script ...

     

$_POST in the PHP Manual

And taking into account that the keys of that array are the values of the attribute name , in the case that what you pass by POST is a form.

So, if you are only interested in the keys, you can use array_keys , to obtain them, since this function:

  

Returns all the keys of an array or a subset of keys of a   array.

     

array_keys in the PHP Manual

Then:

$arrLlaves=array_keys($_POST);

You will create an array only with each value of the attributes name of your form. To read them, you have to do it as the arrays are read.

On the contrary, if you are also interested in the values, you can proceed as indicated by @ Sr1871 in your answer.

    
answered by 13.12.2017 / 22:36
source
5

$_POST brings you an array of all the variables in this way [{name} => {value}];

If you want to get the name you just have to do

foreach($_POST as $name => $value) {
    echo $name; //en tu ejemplo name sería nombre y $value sería perro
}

And you'll get the value of the input name

    
answered by 13.12.2017 в 22:23