Henrique
Henrique

Reputation: 43

Is it possible to put a php variable inside $_POST['']?

I was wondering if it's possible to put a PHP variable inside $_POST[''] like this:

$_POST[$variable];

I'm asking this because I have a page in which all the inputs have dynamic names according to how many orders the user has made and when I must retrieve their values through post, I never know the names but if I would have something like $_POST[$variable] I would know. The variable is a string and so it would turn out like $_POST['String'].

So is it possible to do something like this?

$numero = $count_ficha;
$countU = 1;

for ($i = 1;$i < $numero + 1;$i++) {
    $identificador = "identificadorNI".$countU;
    $identificador2 = "identificador".$countU;
    $id_subencomenda = $_POST[$identificador];
    $countU++;

    echo $id_subencomenda;
}

Upvotes: 2

Views: 5643

Answers (3)

Hely Saul Oberto
Hely Saul Oberto

Reputation: 597

It is posible, but if you don't know the key of $POST[] that you're trying to get the value of, how could you know what value that you have to put on $variable?

For unknow values, the best you can do is a foreach, so you can iterate all the $POST array and then you can do whatever you want with the values, like this

foreach ($_POST as $key) {
   //do whatever you want with the $key
}

Check PHP foreach

Upvotes: 0

colapiombo
colapiombo

Reputation: 189

if you have a dynamic value for the page you just use some $variable because $_POST is use to send information from some HTTP POST method.

(http://php.net/manual/en/reserved.variables.post.php)

maybe you can resolve your problem by creating a global variable.

Upvotes: -1

JJT
JJT

Reputation: 186

Yes, it is possible to use a variable inside $_POST[''] such as $_POST[$variable]

You will still be required, however, to define a value for $variable

Upvotes: 3

Related Questions