On Thu, 04 Dec 2008 08:51:12 GMT, pbryd@hotmail.com wrote:
>
> >
> > Can someone point me in the right direction to make a start?
> >
> > Phil
>
> I've made a start and got a working version using the code below, I
> could do with a amendment to the code to round up the $total to a full
> number.
>
> > $x = $_POST['width'];
> $y = $_POST['length'];
> $a = $_POST['packsize'];
> ?>
> You entered width:
> You entered length:
> You entered pack size:
> > $total = $x * $y / $a;
> echo "$total" ?>
You might want to consult various tutorials, especially concerning
security, if you continue to deal with Web app programming.
Here are a few improvements to help catch errors caused by user input
and sanitize user input:
// keep track of errors
$error = array();
// get and sanitize data, making sure it exists first
$x = isset($_POST['width']) ? floatval($_POST['width']) : 0;
$y = isset($_POST['length']) ? floatval($_POST['length']) : 0;
$a = isset($_POST['packsize']) ? floatval($_POST['packsize']) : 1;
$total = 0;
// calculate data, provided we don't divide by 0
if ($a == 0)
$error[] = 'Error: division by zero.';
else
$total = ceil($x * $y / $a);
// check if form was submitted and that we have no errors
// if we do have errors, just output them and don't calculate
if (!empty($_POST) && !$error) {
echo "Width: $x
\n"
. "Length: $y
\n"
. "Pack size: $a
\n"
. "Total: $total\n";
} else {
// list error(s)
echo "
\n";
foreach ($error as $msg)
echo "\t- $msg
\n";
echo "
\n";
}
?>
A little note about isset() and floatval(): it is good practice to
check that an array index exists before using it, so we use isset()
to check. We use floatval() to ensure that we don't output the user
input unsanitized. If they decided to use HTML characters, for
whatever reason (XSS, in some cases), your version would output them
directly. floatval() serves to return anything not numeric as 0.
Good luck.
--
Curtis
$email = str_replace('sig.invalid', 'gmail.com', $from);