Page 1 of 1

Create dynamic variable names in script form with new PHP

PostPosted: 02. April 2014 08:39
by AAA123
Hi Everybody smart!
The case concerns the late determining dynamic variable names in script form that contains new versions of PHP

Variable is the created name of input field type=checkbox in the form defined as: $opcja_ row_z [0]
where $row_z [0] is unknown generated somewhere else. (ie. 29, 311, 108 ...etc).
Here for a simplicity we assumed $row_z [0] = 29;

[size = 2] This script works well in WAMP5 (Apache1.3, PHP 5.04)
while in XAMPP 1.8 (Apache2.4, PHP 5.47) does not recognize
interpreted the variable name opcja_29 received
from checkbox form [/size]
When I mark the checkbox on and send the form I got the folliwing on the screen:

$_GET Array ( [opcja_29] => on )
opcja_29 : // <--- HERE SHOULD BE opcja_29 : on -- BUT NOT, WHY ? ? ?

Notice: Undefined variable: opcja_29 in C:\xampp-portable\htdocs\4-00.php on line 12

The state of checkbox "on" is delivered in $_GET but I cannot display it.
I may do echo $_GET['opcja_29'] , but "opcja_XX" is generated dynamicly, (so no solid variable name)
Could anyboby help to fix the issue?
And here is the code "4-00.php"
Code: Select all
<HTML><HEAD><TITLE>Formularz</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-2">
</HEAD><BODY>
<?php                    $row_z[0]=29;   
      $tmp = "opcja_$row_z[0]";

if(isset($_GET[$tmp]))
{ $temp = $_GET[$tmp];}else{ $temp = ""; }
     print_r($_GET);

  if ($temp) {
     echo"<br> opcja_$row_z[0]"; echo" : ";
     echo ${"opcja_$row_z[0]"};    
     print '<br><A HREF="4-00.php">Powr√≥t do formularza</A>';
  } else {           
    echo "<br> No data entered  <br>";
   
    print '<FORM ACTION="4-00.php" METHOD=GET>';
    //  print '<INPUT TYPE="text" NAME="opcja_29">';
    print("<input type=checkbox name=\"opcja_$row_z[0]\" >");
         print '<INPUT TYPE="submit" VALUE="Send">';
         print '</FORM>';
     }

 ?></BODY></HTML>