Batting average solution

(New page: <pre> <?php - * Created on Mar 10, 2008 * battingaverage.php presents a form for calculating * batting average as hits/at_bats. *: ?> <HTML> <HEAD> <TITLE>Building a Form</TITLE...)
 
Line 42: Line 42:
</HTML>
</HTML>
</pre>
</pre>
 +
 +
[[Category:Sp08]]

Current revision as of 13:18, 19 September 2008

<?php
/*
 * Created on Mar 10, 2008
 * battingaverage.php presents a form for calculating
 *  batting average as hits/at_bats.
 *
 */
?>
<HTML>
<HEAD>
  <TITLE>Building a Form</TITLE>
</HEAD>
<BODY>
<H4>Calculate Your Batting Average</H4>
<?php
$battingaverage = 0;
$atbats	= htmlentities($_GET["atbats"]);    // Get the data stored in the form's search element
$hits	= htmlentities($_GET["hits"]);    // Get the data stored in the form's search element
$self =	htmlentities($_SERVER['PHP_SELF']); // This file itself will process the form.
echo ('
    <FORM ACTION="'.$self.'"  METHOD="GET">
    <LABEL>
        At bats: <INPUT TYPE="TEXT" NAME="atbats" VALUE="'.$atbats.'" />
        Hits: <INPUT TYPE="TEXT" NAME="hits" VALUE="'.$hits.'" />
   </LABEL>
    <INPUT TYPE="Submit" VALUE="Calculate Batting Average!" />
    </FORM>');

if ($atbats ===	'' || $hits === '') {
   echo	"Please input at bats and hits!<BR>";
} else {
   $battingaverage = calculateBA($atbats,$hits);
   printf ("Your current batting average is <strong>%0.3f</strong>", $battingaverage);
}

function calculateBA($atbats, $hits){
	return $hits/$atbats;
}
?>
</BODY>
</HTML>