Hi
Im trying to create a form that will add two numbers and input the answer automatically into a text box. Please help.
Basically, I have created two text boxes called length and width and another called total. So if I entered 5 in width and 50 in Length, I want the answer displayed automatically in Total. How would I do that.
Appreciate it
PHP Calculator
Started by hitman123, Jul 20 2009 12:43 AM
5 replies to this topic
#1
Posted 20 July 2009 - 12:43 AM
|
|
|
#2
Posted 20 July 2009 - 02:19 AM
there is two ways, either, you submit it to a php script which calculates and shows the result, or you do a javascript that calculates it.
easiest with php is that you read the posted form and if there is values, you show the total, otherwise not.
easiest with php is that you read the posted form and if there is values, you show the total, otherwise not.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#3
Posted 20 July 2009 - 03:06 AM
The description sounds like it requires JavaScript.
#4
Posted 20 July 2009 - 03:35 PM
If you want it to show up in a textbox, you would most probably have to use JavaScript. If you want it to show a different page, you would use PHP.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
#5
Posted 20 July 2009 - 04:33 PM
<html>
<head>
<title>Calculator</title>
</head>
<body>
<input type="text" id="one" name="one" /><input type="text" id="two" name="two" /><br /><input type="submit" onclick="adder()"><div id="three"> </div>
<script>
function adder() {
var one = parseInt(document.getElementById('one').value);
var two = parseInt(document.getElementById('two').value);
document.getElementById('three').innerHTML = one+two;
}
</script>
</body>
</html>
#6
Posted 28 July 2009 - 12:07 PM
Well i have created one.
It's simpel but it works :)
First the javascript:
Then HTML:
Hope you can get it work :p
It's simpel but it works :)
First the javascript:
<script type="text/javascript">
function pushButton(buttonValue) {
if (buttonValue == 'C') {
document.getElementById('screen').value = '';
}
else {
document.getElementById('screen').value += buttonValue;
}
}
function calculate(equation) {
var answer = eval(equation);
document.getElementById('screen').value = answer;
}
function Select(){
document.calc.calc.focus();
}
</script>
Then HTML:
<form name="calc">
<table class="calc" cellpadding="0">
<tr>
<td colspan="4"><input class="calc" id="screen" name="calc" type="text" accesskey="s"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=1 onclick="pushButton(this.value);" accesskey="1"></td>
<td><input class="calc" type="button" value=2 onclick="pushButton(this.value);" accesskey="2"></td>
<td><input class="calc" type="button" value=3 onclick="pushButton(this.value);" accesskey="3"></td>
<td><input class="calc" type="button" value='/' onclick="pushButton(this.value);" accesskey="d"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=4 onclick="pushButton(this.value);" accesskey="4"></td>
<td><input class="calc" type="button" value=5 onclick="pushButton(this.value);" accesskey="5"></td>
<td><input class="calc" type="button" value=6 onclick="pushButton(this.value);" accesskey="6"></td>
<td><input class="calc" type="button" value='*' onclick="pushButton(this.value);" accesskey="*"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=7 onclick="pushButton(this.value);" accesskey="7"></td>
<td><input class="calc" type="button" value=8 onclick="pushButton(this.value);" accesskey="8"></td>
<td><input class="calc" type="button" value=9 onclick="pushButton(this.value);" accesskey="9"></td>
<td><input class="calc" type="button" value='-' onclick="pushButton(this.value);" accesskey="-"></td>
</tr>
<tr>
<td><input class="calc" type="button" value=0 onclick="pushButton(this.value);" accesskey="0"></td>
<td><input class="calc" type="button" value='.' onclick="pushButton(this.value);" accesskey=","></td>
<td><input class="calc" type="button" value="PI" onclick="pushButton(3.141592653589793);" accesskey="p" id="info-pi-o"></td>
<td><input class="calc" type="button" value='+' onclick="pushButton(this.value);" accesskey="+"></td>
</tr>
<tr>
<td colspan="2"><input class="calc" type="button" value='=' onclick="calculate(document.getElementById('screen').value);" accesskey="l" id="info-lig-o"></td>
<td colspan="2"><input class="calc" type="button" value='C' onclick="pushButton(this.value);" accesskey="c" id="info-clear-o"></td>
</tr>
</table>
</form>
Hope you can get it work :p


Sign In
Create Account


Back to top










