Hey all, this may seem abit silly but im having real trouble working out how to make a php form that subtracts and adds numbers from 3 different box's into a new box..

abit like this...

box 1:
10
box 2 adds:
1
box 3 minus:
5

Result:
6


im guessing its really simple but im extremely tired and ive been at it for about 3 hours :(
might cry

someone help?

Here is the basic code.

<?php
if (!empty($_GET['a']) && !empty($_GET['b']) && !empty($_GET['c'])) {
    $result=(int) $_GET['a']+ (int) $_GET['b'] - (int) $_GET['c'];
    echo 'Answer: '.$result;
}
?>
<form method="GET">
Total: <input type="text" name="a"><br>
Add: <input type="text" name="b"><br>
Minus: <input type="text" name="c"><br>
<input type="submit" value="Calculate">
</form>

Also you may want to read up on a few php tutorials. The link in my signature contains some useful tutorials.

This is the simple script to do the same-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Calc</title>
<script type="text/javascript" language="javascript" >
function calculate()
{	
	value_1 = document.getElementById("val1").value;
	value_2 = document.getElementById("val2").value;
	value_3 = document.getElementById("val3").value;
	alert(parseInt(value_1)+parseInt(value_2));
	document.getElementById("val4").value = (parseInt(value_1)+parseInt(value_2)) - parseInt(value_3);
}
</script>
</head>

<body>
<form name="frm" id="frm" method="post">
<table>
<tr>
<td><input type="text" value="4" id="val1" /></td>
<td><input type="text" value="4" id="val2"/></td>
<td><input type="text" value="4" id="val3"/></td>
<td><input type="text" value="" id="val4"/></td>
</tr>
<tr><td><input type="button" onclick="calculate();" /></td></tr>
</table>
</form>
</body>
</html>
Member Avatar for diafol

Although I don't particularly like js, I think it would be useful for a calculator as you don't need to involve the server for a trivial function.

thanks guys, but hate to say, about an hour after posting this thread, i did it all myself

exaclty like yours cwarn23

thanks anyways guys :)

<form method="GET">
Total: <input type="text" name="a" <?php if (!empty($_GET['a']) echo "value='$_get['a']'"; ?>><br>
Add: <input type="text" name="b" <?php if (!empty($_GET['b']) echo "value='$_get['b']'"; ?>><br>
Minus: <input type="text" name="c" <?php if (!empty($_GET['c']) echo "value='$_get['c']'"; ?>><br>
<input type="submit" value="Calculate">
</form>
<?php if (!empty($_GET['a']) echo $_get['a']; && !empty($_GET['b']) && !empty($_GET['c'])) {
    $result=(int) $_GET['a']+ (int) $_GET['b'] - (int) $_GET['c'];
    echo 'Answer: '.$result;
} ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.