What will you learn
You are going to learn how to make a functioning input calculator!
And this one will work!
Who should be reading this
It might help if you have some previous knowledge of php before we get into this...
------
Ok, let's begin!
To start off, make a new file and call it calculator.php
Here's the script with full comments. Don't like it? Then don't copy and paste it noob.
==============
<?php // it's just the beginning, calm down....
if (isset($_POST['submitted'])) {
// This just makes sure the form is submitted, so it doesn't display a blank or "empty" script.
// This next part makes sure that the entered values into the input boxes are numbers, and it's also just an if statement as well,
if ( is_numeric9$_POST['num1']) && is_numeric($_POST['num2']) ) {
if ( $cal == "add" ) { echo "$num1 + $num2 = "; echo "Your Total is: $num1 + $num2";}
if ( $cal == "sub" ) { echo "$num1 - $num2 = "; echo "Your Total is: $num1 - $num2";}
if ( $cal == "mul" ) { echo "$num1 * $num2 = "; echo "Your Total is: $num1 * $num2";}
if ( $cal == "div" ) { echo "$num1 / $num2 = "; echo "Your Total is: $num1 / $num2";}
// This just tells the user what the value of total is. Total being the +-*/ of num1 and num2 of the fields below
// This also makes an if statement makeing the selection you made and doing the math with the variables you provided
}else{ // a simple else statement for the if above
echo 'Please enter a valid number for the number fields and Choose a Sign.';
// This just tells the user that there was an error.
}
} // End the if and else
?> // really, if you don't know this one, you shouldn't be reading this...
<h1>A Freakin' Calculator!!</h1>
<form action="calculator.php" method="post">
<!-- Be sure to rename calculator.php to whatever you have this script saved as... -->
<table align="center" cellpadding="5" cellspacing="0">
<!-- Just some organizing taking place -->
<tr><td>First Number: </td><td><input type="text" size="25" maxlength="20" name="num1" /></td></tr>
<tr><td>Second Number: </td><td><input type="text" size="25" maxlength="20" name="num2" /></td></tr>
<tr><td>Sign: </td><td><select name="cal">
<option name="cal" value="add" >ADDITION</option>
<option name="cal" value="sub" >SUBTRACTION</option>
<option name="cal" value="mul" >MULTIPLICATION</option>
<option name="cal" value="div" >DIVISION</option>
</select>
<tr><td colspan="2"><input type="submit" value="Calculate!" name="submit" /></td></tr>
<input type="hidden" name="submitted" value="TRUE" />
</table>
</form>
<!-- Ok, big script, all this does is displays an input field, 3 to be exact, with the values that we stated above, num1, num2, sign and gives a form.
It also makes the invisible input field submitted, true. So that the above script can work properly. -->
<!-- END OF PAGE -->
So there you have it. A nice little calculator. It's simple, it's easy, and it's nicely conformed to modifications.
This is a tool for fun. Please don't be over drastic about this and think it's perferct. Nothing is perfect. Make your own modifications. Don't just copy and paste this code.
If you are going to use it on a working website, i suggest you modify it to your liking.
This is just an example, and may not have everything you like. If you want something added to it......don't ask me. Do it yourself.
Thanks for caring.
- Momo