00:00
00:00
Newgrounds Background Image Theme

HatedExpert just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

PHP: Calculator

1,063 Views | 6 Replies
New Topic Respond to this Topic

PHP: Calculator 2006-08-05 01:34:01


PHP: Main -- Gosh this is really annoying with the copy and paste and then copy and then go back and then a tags....SHEESH

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


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to PHP: Calculator 2006-08-05 01:37:57


Momo!! I specifically told you to test it BEFORE posting it.

You have numerous errors. In the PHP script, where do you get $cal from?

Your missing a parenthesis when you do is_numeric.

:(

Response to PHP: Calculator 2006-08-05 01:39:15


At 8/5/06 01:37 AM, GamesCool wrote: Momo!! I specifically told you to test it BEFORE posting it.

You have numerous errors. In the PHP script, where do you get $cal from?

Your missing a parenthesis when you do is_numeric.

(

stupid local isn't working, and neither are my 3 accounts at 1500mb.com. SO i had no choice, i couldn't test,

$cal is from the name of the select crap.


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to PHP: Calculator 2006-08-05 01:40:19


whoops, sorry for double post, but the 9 is supposed to be a ( lol.

I slipped i guess. :/ it's late anyaway.


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to PHP: Calculator 2006-08-05 01:40:38


At 8/5/06 01:39 AM, Momo_the_Monkey wrote: $cal is from the name of the select crap.

but you never declared it locally in the script or used $_POST['cal'] :(

You should have PMed me it and I could have tested it for you. Again, we can't use this as it has too many errors.

Response to PHP: Calculator 2006-08-05 01:41:34


At 8/5/06 01:40 AM, GamesCool wrote:
At 8/5/06 01:39 AM, Momo_the_Monkey wrote: $cal is from the name of the select crap.
but you never declared it locally in the script or used $_POST['cal'] :(

You should have PMed me it and I could have tested it for you. Again, we can't use this as it has too many errors.

i'm retarded. I'll repost it tomorrow. Error free, insane flame free.


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to PHP: Calculator 2006-08-05 01:42:27


At 8/5/06 01:41 AM, Momo_the_Monkey wrote: i'm retarded. I'll repost it tomorrow. Error free, insane flame free.

OK, sounds good :)