Hello, "Richard Meredith-Wilks" <[EMAIL PROTECTED]> wrote in message B1CFE9307BACD211B0710000D11C344102A4E8E0@UKSTONTSRV3">news:B1CFE9307BACD211B0710000D11C344102A4E8E0@UKSTONTSRV3... > > Hi, > > > > I have a maths question. > > > > I'm trying to create a html form where a user can enter formula in an > > input box (e.g. '$a + $b - 10') and posts it to PHP. > > The variables $a and $b are known but the formula can be changed on the > > form (e.g. '$a / $b * 2'). > > I want PHP to take the text input, the formula and calculate the result > > and display the results.
<form name="maths" action="<? echo $PHP_SELF; ?>" method="Post"> <input type="text" name="a" size="4" maxlength="8"> <select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" name="b" size="4" maxlength="8"> <input type="hidden" name="action" value="Go"> <input type="submit" name="Submit" value="Calculate"> </form> <?php if($action) { function CalculateValues($a, $operator, $b) { if ($operator == "+") { $result = $a + $b; } else if ($operator == "-") { $result = $a - $b; } else if ($operator == "*") { $result = $a * $b; } else { // Can't divide by 0 if ($a == 0) { $result = 0; } else { $result = $a / $b; } } return $result; } $operator = stripslashes($operator); // May not need this. $operator_array = array("+", "-", "*", "/"); if ($a == "") { echo "No value given for a"; } else if (!in_array($operator, $operator_array)) { echo "No operator chosen."; } else if ($b == "") { echo "No value given for b"; } else { $result = $a . $operator . $b; echo "$a $operator $b=" . CalculateValues($a, $operator, $b); } } ?> If the values are known, just hard code them. If not, you'll want to pay closer attention to the user input. It's not fully what you're after, but it's enough to get you started anyway. James -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php