How to get value $ _POST automatically?

0

I have this form.

<form>
   <input type="text" name="nome">
</form>

Would you like to get the value of $_POST in php automatically?

I need to get the data from a form where I do not know the field names or quantity.

<form method="post">
   <input type="text" name="nome">
   <input type="text" name="outro-campo">
   <input type="text" name="outro-campo2">
</form>

Instead of doing

echo $_POST['nome'];
echo $_POST['outro-campo'];
echo $_POST['outro-campo2'];

I need something automatic but I do not know how.

    
asked by anonymous 17.07.2017 / 16:52

3 answers

3

If I understood correctly, your question would look like this:

<form>
   <input type="text" name="nome">
</form>

foreach($_REQUEST as $key => $value) {
    if($key == 'nome') {
        echo $value;
    }
}
    
17.07.2017 / 19:00
2

I think what you want is to try to create normal variables based on the values that $_POST has.

Although this is not a good idea because it can open security holes , you can do this as follows:

foreach ( $_POST as $chave => $valor ) { $$chave = $valor; }

That creates a variable with the name that comes in $_POST and for each value that comes there. Then just use them as if they were normal variables:

echo ($nome);

Alternatively, name creation can be done at the expense of the extract function as Anderson Carlos Woss and rray have suggested. To do this, just call the function:

extract($_POST, EXTR_PREFIX_SAME, "prefixo");

The parameter EXTR_PREFIX_SAME indicates that if there is a name collision a prefix must be created, with the text relative to the 3rd parameter, which in the above example was "prefixo" ,

Example running

    
17.07.2017 / 17:24
0

It was something like that that I wanted

print_r( $_POST);

foreach($_POST as $field => $value)
{
    echo $field;
}
    
17.07.2017 / 17:09