Form response evaluation II

In some previous task, we have tried the $_GET and $_POST variables, the arrays, containing all the form response. As you remember, if we name a form input element, for example, "alpha":

<input name="alpha" type="text" size="20">

... then - using the GET method - we can read the inserted value as  $_GET["alpha"]   (it could be used everywhere, where can be a variable). This kind of array, which is addressed by the names of the element, is so called associative. It should be noticed, that in php we can still use an index (integer value, the first element has the 0 address) for a direct addressing.

In the php (and the perl) language, we have better solution to go through all of the elements, if we don't know their names: the special form of the cycle command, the Foreach. For the GET form response, it could be used in the simple form:

<?
foreach($_GET as $value)
{
    echo $value."<br>";
  }
?>

Simple, fast and reliable. But, we can use the complete form of the command, and read the element names (so called keys) as well. This cycle is defined as:

foreach(<<source array>> as <<key>> => <<value>>) {the-cycle-body};

It will read each array element and for each of them it will set the <<key>> and <<value>> variable and execute the cycle body with this values set. Note the => sign; it is just an arrow. Comparision of two values in php is >= (in this order), like in the C++.

For this listing on a html page, it will be usefull to create a table. We should write a table header (colored blue), then list of an associative array, and then close the table (colored violet):

<table border><tr><td>key</td><td>value</td></tr>
<?
 foreach($_GET as $key => $value){
   echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
}
?>
</table>

It is usefull only for debuging purposes, but the foreach cycle could be used for a database response, where we cannot assume, how many lines is in the response.

If we like to create an universal form response (for example with the name of "debug.php"), we should list not only the GET, but the POST content as well. If the form response is in the $_GET array, we can check using:

if($_GET == true)

If there is no element in the array, it returns the "false" value. Using Ctrl+C and Ctrl+V, we will repeat it for the POST array. The complete debug.php script could look similar, like this:

<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1250">
<title>Universal form response</title>
</head>

<body bgcolor="#FFFFFF">

<p><font size="4"><strong>This data has been passed on:</strong></font></p>

<p><strong>Post method:</strong> <br>
<?php
if($_POST == true)
{
  echo "<table border><tr><td>key</td><td>value</td></tr>";
  foreach($_POST as $key => $value){
    echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
  }
  echo "</table>";
}



?></p>
<p><strong>Get method:</strong> <br>
<?php
if($_GET == true)
{
  echo "<table border><tr><td>key</td><td>value</td></tr>";
  foreach($_GET as $key => $value){
    echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
  }
  echo "</table>";
}


?></p>

</body>
</html>

Try it and keep it, it could be usefull when you debug a form. If you like, you can use this universal form on my web page, simply using
<form action="http://iat.fs.cvut.cz/web/cv/post3.php" method="...."> ... ; after you will check, if the data is correct, simply change the action to your php script.