Jump to content

PHP Calculator

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
hitman123

hitman123

    Newbie

  • Members
  • PipPip
  • 11 posts
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

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The description sounds like it requires JavaScript.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
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
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
<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
fyhring4

fyhring4

    Newbie

  • Members
  • PipPip
  • 13 posts
Well i have created one.
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