6.4 More Examples
6.4.2 Reading HTML checkbox Values
This code is easier to write with consecutive integer keys than with arbitrarily named keys, but that approach would essentially defeats the purpose of simplifying access to form fields, which the example in Docu- ment 6.11 was originally intended to demonstrate.
You might conclude that the code presented in these examples is not much of a simplification and is not worth the extra effort, because the PHP application still needs to“know” the name of the coefficient array entered into the HTML document’s form fields. However, if only one name is needed—the name of that array—this approach might provide some code-writing economy for a longer list of inputs.
<br />
<formmethod="post"action="CloudObs.php" />
<table>
<tr>
<td><strong>High</strong> </td>
<td>
<inputtype="checkbox"name="high[]"
value="Cirrus" />Cirrus</td>
<td>
<inputtype="checkbox"name="high[]"
value="Cirrocumulus" />Cirrocumulus</td>
<td>
<inputtype="checkbox"name="high[]"
value="Cirrostratus" />Cirrostratus</td></tr>
<tr>
<tdcolspan="4"><hrnoshade color="black" />
</td></tr>
<tr>
<td> <strong>Middle</strong> </td>
<td>
<inputtype="checkbox"name="mid[]"
value="Altostratus" />Altostratus</td>
<td>
<inputtype="checkbox"name="mid[]"
value="Altocumulus" />Altocumulus</td></tr>
<tr>
<tdcolspan="4"><hrnoshade color="black" />
</td></tr>
<tr>
<td> <strong>Low</strong></td>
<td>
<inputtype="checkbox"name="low[]"value="Stratus" />
Stratus</td>
<td>
<inputtype="checkbox"name="low[]"
value="Stratocumulus" />Stratocumulus</td>
<td>
<inputtype="checkbox"name="low[]"value="Cumulus" />
Cumulus</td></tr>
<tr>
<tdcolspan="4"><hrnoshade color="black" />
/td></tr>
<tr>
<td> <strong>Rain-Producing</strong> </td>
<td>
<inputtype="checkbox"name="rain[]"
124 6 Arrays
value="Nimbostratus" />Nimbostratus</td>
<td>
<inputtype="checkbox"name="rain[]"
value="Cumulonimbus" />Cumulonimbus</td></tr>
</table>
<inputtype="submit"value="Click to process…" />
</form>
</body>
</html>
It is very easy to process these data with PHP if the HTML document is written correctly, as shown in Document 6.13a. Each cloud category—high, mid, low, or precipitating—must be specified as an array,$high [] rather than just$high, for example. You do not need to specify the index values.
The$_POST […]operation performed in PHP will return an array including just those cloud types that have been checked. That is, PHP automatically does the work that you would otherwise have to do yourself to separate checked boxes from unchecked ones. But…
If you don’t check any boxes for a cloud category, then no array is created for that category and your code must provide a way to see if this has happened. Not surprisingly, there is a helpful PHP function for this task. For example: if (isset($_POST ["high"])) returns a value of true if a variable (in this case, an array) exists or false if it doesn’t. The PHP code for this problem is in Document 6.12b.
Document 6.12b (cloudObs.php)
<?php
// high clouds
if(isset($_POST["high"])) {
$high=$_POST["high"];
$n=count($high);
echo"For high clouds, you observed <br />"; for($i=0;$i<$n;$i++)
echo $high[$i]."<br>"; }
else echo"You didn't observe any high clouds.<br />"; // mid clouds
if(isset($_POST["mid"])) {
$mid=$_POST["mid"];
$n=count($mid);
echo"For mid clouds, you observed<br />"; for($i=0;$i<$n;$i++)
echo $mid[$i] ."<br />"; }
else echo"You didn't observe any mid clouds.<br />"; // low clouds
if(isset($_POST["low"])) {
$low=$_POST["low"];
$n=count($low);
echo"For low clouds, you observed<br />"; for($i=0;$i<$n;$i++)
echo $low[$i] ."<br />"; }
else echo"You didn't observe any low clouds.<br />"; // rain clouds
if(isset($_POST["rain"])) {
$rain=$_POST["rain"];
$n=count($rain);
echo"For rain clouds, you observed<br />"; for($i=0;$i<$n;$i++)
echo $rain[$i] ."<br />"; }
else echo"You didn't observe any rain clouds.<br />";
?>
The number of boxes checked for each category is contained in the value of $n. (Depending on your needs, you could create a different variable name for each category.) For mid and low clouds, no boxes are checked, so their corresponding
arrays are empty and theirfor…loops are not executed.
126 6 Arrays