2

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;
}
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
Henrique
  • 43
  • 1
  • 6

3 Answers3

3

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

JJT
  • 186
  • 1
  • 13
  • 2
    Why the down vote for this answer? It is technically correct. I only ask so that I can better understand what my error is... – JJT Sep 18 '17 at 13:55
0

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

Hely Saul Oberto
  • 577
  • 1
  • 10
  • 22
-1

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.

Mehul Kuriya
  • 608
  • 5
  • 18
colapiombo
  • 179
  • 1
  • 3
  • 13