kaseencook Posted March 30, 2009 Share Posted March 30, 2009 Hi! I sell a custom product and the price is determine by the internal surface area of a rectangular mold. I would like to make a form in conjunction with PHP so that people can enter their mold's length, width and height and get a value for the internal surface area, and therefore know what product to buy. This is what I have so far: The HTML form, calculate_form.html: <form method="post" action="calculate.php"> <p>Internal Length of Mould: <input name="l" size="10" type="text"></p> <p>Internal Width of Mould: <input name="w" size="10" type="text"></p> <p>Internal Height of Mould: <input name="h" size="10" type="text"></p> <p><input name="submit" value="Calculate" type="submit"></p> AND the PHP script, calculate.php: <?php if (($_POST[l] == "") || ($_POST[w] == "") || ($_POST[h] =="")) { header("Location: calculate_form.html"); exit; } $result = (($_POST[l]*(($_POST[h]*2)+$_POST[w])+(2*($_POST[w]*$_POST[h]))) } echo "<title>Calculation Result</title>"; echo "<p>The result of the calculation is: $result</p>"; echo "<p><a href=\"calculate_form.html\" target=\"_self\">Do Another</a></p>"; ?> I greatly appreciate your help with modifying this puppy to work (in advance) :) -Kas Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/ Share on other sites More sharing options...
Daniel0 Posted March 30, 2009 Share Posted March 30, 2009 A rectangular cuboid has three pairs of identical faces: height*length, depth*height, depth*length. This means the surface area is given by 2hl+2dh+2dl. Put into PHP this gives you: $surfaceArea = 2*$_POST['h']*$_POST['l']+2*$_POST['d']*$_POST['h']+2*$_POST['d']*$_POST['l']; You can add parentheses if you think it makes it more readable, but its sort of redundant because because multiplication takes higher precedence than addition. It would be better if you named the sides like that. It's sort of ambiguous which side if which if you have something called "width" and "length". You normally say that you have the two dimensions "width" and "height", and then you can add the third dimension "depth". Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-796815 Share on other sites More sharing options...
kaseencook Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks for you help! I probably should have been more specific. The SA is without the top side, but your equation is much simipler than the one I had before so I will definately use SA= 2lh+2wh+wl. My web page includes a diagram of what defines length, with, and height - this is the page I want to make a calc form for (http://www.tortugasoaps.com.au/customliners.html). I put in the new equation organisation but I still can't get the form+PHP to work. I have never worked in PHP before, so I just did a lot of reasearch and put together the best approximation of what I thought could work, but there must be something missing becuase it doesn't work. Here is a link to the test pages that I have been working on. The form: http://www.ccaretta.com/tortuga/calculate_form.html after submitting the values, it then goes to the PHP form and it is blank...... I just can't find any similar code to model it after (I've searched here and on the net). CHEER! Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797416 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 What is the PHP code on that page? Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797423 Share on other sites More sharing options...
kaseencook Posted March 31, 2009 Author Share Posted March 31, 2009 I think I got it to work! (but still some minor errors) FORM: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><input> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Calc</title> </head> <body> <!---Calculator--> <form method="post" action="calculate.php"> <br> Internal Length of Mould(cm): <input name="length" size="10" type="text" <br> Internal Width of Mould(cm): <input name="width" size="10" type="text" <br> Internal Height of Mould(cm): <input name="height" size="10" type="text" <br> <INPUT TYPE=RESET NAME=RESET VALUE="Reset"> <input name="submit" value="Calculate" type="submit"> </form> <!---endcalc---> <br> </body></html> and the PHP code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><input> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Calc</title> </head> <body> <?php $length =$_POST['length']; $height =$_POST['height']; $width =$_POST['width']; $surfacearea = 2*$length*$height+2*$width*$height+$width*$length; // Print out the results: print "<html><pre>"; print "The Length (cm): $length.\n)"; print "The Width (cm): $width.\n"; print "The Height (cm): $height.\n"; print "The total internal surface area of your mould (cm2)is: $surfacearea.\n"; print "</pre></html>\n"; ?> <br> </body></html> I looked at some more PHP tutorials and whacked together some more simple ways of doing the PHP from how I first did it, and it seems to work now, except there is a form field that seems to hover at the top of the page but isn't in the code ??? Also, do you know how I can combine the HTML form and PHP into one file/page? I am doing a bit of reading on it now.... humm..... hoping it is something simple and logical Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797489 Share on other sites More sharing options...
corbin Posted March 31, 2009 Share Posted March 31, 2009 You forgot closing > on all of your input tags x.x. <input name="length" size="10" type="text"> Also, you should technically make sure the inputs are numeric. You could use is_numeric. Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797494 Share on other sites More sharing options...
Daniel0 Posted March 31, 2009 Share Posted March 31, 2009 Also, you should technically make sure the inputs are numeric. A value will implicitly be cast as integer or float if you perform arithmetic operations on them. Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797550 Share on other sites More sharing options...
kaseencook Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks for that Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797573 Share on other sites More sharing options...
Daniel0 Posted March 31, 2009 Share Posted March 31, 2009 Actually, you might want to explicitly cast the values as floats considering you are outputting those values as well. I didn't see that. So like $length = (float) $_POST['length']; etc. Quote Link to comment https://forums.phpfreaks.com/topic/151718-need-some-help-with-equation-in-php/#findComment-797576 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.