Hi there,
I am experimenting with a form to calculate volumes by multiplying the 3 inputs together (lengths, width & thickness). I would like help to figure out how to validate the form so only numeric/integer values can be used, also to add selectable units of measurements to the inputs so that the output of the form shows correctly. for example mm(millimeters), "(feet), (m)metres and so on…also the cubic value symbol (small 3).
I would appreciate any help available with the above, but first:
When I submit my current form, I get no errors however…it always outputs a “0”(zero). I am primarily requiring guidance on what I have done wrong in my code and how it can be improved?
here is the code:
[php]
Conversions.php <?php //Metric Length Variables $mm = 1; $m = 1; $mm_per_cm = 10; $mm_per_m = 1000; $cm_per_m = 100; //Imperial Length Variables $inch = 1; $foot = 1; $yard = 1; //Specific Metric & Imperial Length Conversions and comparisons $mm_per_inch = 25.4; $cm_per_inch = 2.54; $twelve_inches = ($inch * 12); $mm_per_foot = ($foot * $mm_per_inch);//There are 12 inches in a foot and 25.4mm per inch, so: 12 x 25.4mm = 304.8mm per foot $mm_per_yard = ($mm_per_foot * 3);// There are 304.8mm per foot and 3 feet per yard, so; 304.8 multipleied by 3 =914.4mm $inches_per_ft = ($mm_per_foot / $mm_per_inch);//There are 304.8mm per foot and 25.4mm per inch, so: 304.8 divided by 25.4 =12inches per foot $inches_per_yard = ($twelve_inches * 3);//There are 3 feet per yard and 12 inches per foot, so: 12 multiplied by 3 = 36 inches $inches_per_m = ($mm_per_m / $mm_per_inch);//There are 1000mm per metre and 25.4mm per inch, so: 1000 divided by 25.4 =39.37007874015748 Inches per metre $feet_per_m = ($mm_per_m / $mm_per_foot);//There are 1000mm per metre and 304.8mm per foot, so: 1000 divided by 304.8 =3.280839895013123 feet per metre $yards_per_m = ( ($cm_per_m / $m) * ($inch / $cm_per_inch) * ($yard / $inches_per_yard) ) ; // (100cm divided by 1m =100) * (1 inch divided by 2.54cm =0.3937007874015748) * (1 yard divided by 36 inches = 0.0277777777777778), so the result of these 3 multiplied together is: 1.0936133 Yards per metre $radius = ""; $pi = 3.1416; $area_circle = $radius * $radius * $pi; $circumference = (2 * $radius) * $pi; $length = "length"; $width = "width"; $thickness = "thickness"; $volume = ($length * $width * $thickness); // this is the html entity code for degrees ° ?> [/php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Volume Calculator</title>
</head>
<body>
<form action="volume_calculator.php" method="post">
Length:
<input name="length" type="text" value="" size="12" />
Width/Depth:
<input name="width" type="text" value="" size="12" />
Thickness:
<input name="thickness" type="text" value="" size="12" />
<input name="calculate" type="submit" value="Calculate" />
</form>
</body>
</html>
[php]
Volume Calculator <?php $lengthErr = "You are required to enter a numeric value in each field to calculate the volume!"; include "conversions.php"; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["length"]) ) { print $lengthErr ; } if (empty($_POST["width"]) ) {$widthErr = "A width is required. You must use integer characters only. i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 or any variation containing these numbers";} if (empty($_POST["thickness"]) ) {$thicknessErr = "A thickness is required. You must use integer characters only. i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 or any variation containing these numbers";} else { print ("The volume calculated from your input measurements is " . $volume . " cubic") ; } ; } ?>[/php]
regards,
Luke