How to get $_POST value automatically?

Asked

Viewed 2,942 times

0

I have this form.

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

It would take the value of $_POST in php automatically?

I need to get the data from a form where I don’t know the names of the fields or the amount.

<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 don’t know how.

  • Could you please explain better what you need? your question is not clear enough.

  • 1

    If there were, it would be like trading six for half a dozen.

  • 1

    What do you mean automatically? Magically go to another page and already drop to (example) a variable called $name?

  • 2

    I could not understand very well what you want ... it seems to me that the nearest would be the use of function extract() it converts an array into variables (the names are the keys) but if it is not treated well or has not much control of the situation opens security loopholes similar to the Register globals

3 answers

3


If I understood your question correctly it would be this way:

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

foreach($_REQUEST as $key => $value) {
    if($key == 'nome') {
        echo $value;
    }
}

2

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

Although that’s not a good idea because it could open up security loopholes, may do so in the following manner:

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

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

echo ($nome);

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

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

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

Working example

  • You can use the function extract that has exactly this goal. But I see no reason to do it as much as the foreach about $_POST. Open security gaps in the application if not handled well, as commented by rray in the question.

  • 1

    @Andersoncarloswoss, yes I fully agree, it is not a good idea to do this, as I said at the beginning of the answer, but to answer exactly what he asked, would be a way to do it. If I understand the question correctly!

  • 1

    I was negative because it’s not about variable variables.. What is asked is to iterate the array automatically without knowing which fields (indexes) exist in that array. The use of Extract() and variable variables is out of context.

  • @Danielomine It was not what I understood from the question, but I will wait for the author of the post to comment on what it really was looking for, because in fact it was not very clear.

0

It was something like that I wanted

print_r( $_POST);

foreach($_POST as $field => $value)
{
    echo $field;
}
  • almost that, as you can see it returns an array and in that code it picks up the index of $_POST returning my question I wondered if there was any attribute of php that did this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.