How to store the indices that come from a form via $ _POST

2

I'm making a form that sends data to a normal php file, the problem is that I want to receive in that file. Both the values that come via $ _POST and the indexes of these values.

if I have the following input:

<input id="username" name="username" type="text">

and normally in the php would receive:

$username = $_POST['username'];

What I want to do is store the name of the index and its value in different variables.

    
asked by Deeply 14.05.2018 в 16:18
source

2 answers

0

The answer is what A. Cedano tells you:

foreach ($_POST as $k=>$v){
    echo "La clave es $k y el valor es $v".PHP_EOL;
}

Since $ _ POST alone is already an array, it does not make sense to put the values back into another array.

What would probably make the most sense is that you use the foreach to make a

$k = mysqli_real_escape_string ($link , $v);

what gives you some security if you want to insert it into a database

Anyway the most logical answer seems to me that of A. Cedano since with its loop you can extract the values of the keys (or indexes as you call them)

    
answered by 14.05.2018 / 17:00
source
-1
$array=$_POST;
while ($elemento = current($array)) {
    if($elemento=="username")
    {
      $username=$elemento;
    }   

    next($array);
}
    
answered by 14.05.2018 в 16:52