how to retrieve post values ​​with variable names in php

advertisements

I want to use variables instead of a name in post method i already tried this code but it seems it is not right what can i do instead, this is my PHP code

    <?php
$answer="";
 for($i=0 ; $i<2 ; $i++){

   if(isset($_POST['{$i}'])){
$answer.=$_POST['{$i}'].",";
}
else{$answer.="0,";}
}
 echo $answer;
?>

and this is my form :

    <form method="post" action="data.php">
<lable for="1" >1</lable>
<input type="radio" name="1" value="1"/>
<input type="radio" name="1" value="2"/>
<input type="radio" name="1" value="3"/>
<input type="radio" name="1" value="4"/><br/>
<lable for="2" >2</lable>
<input type="radio" name="2" value="1"/>
<input type="radio" name="2" value="2"/>
<input type="radio" name="2" value="3"/>
<input type="radio" name="2" value="4"/>
<input type="submit" name="submit" value="submit" />


You don't need to wrap the variable name like that, just access it directly. It's an Array, which means it supports the idea of keys.

(I'm not 100% sure using integer numbered keys is a good idea, though. You might want to prepend them with a text value to prevent weird bugs)

$_POST[ $i ]


You can dynamically build them with a text prepend like this in html:

<input type="radio" name="radio_1" value="1" />

And then access them like this in PHP:

$_POST['radio_' . $i];