#### Archived

This topic is now archived and is closed to further replies.

# calculator script

## Recommended Posts

[i]Q1:Create a calculator script that allows the user to submit two numbers and choose an operation to perform on them (addition, multiplication, division, subtraction).

Q2:Use hidden fields with the script you created in exercise 1 to store and display the number of requests that the user has submitted.[/i]

[b]the script which i found in other site:[/b]

<?php
/*Create a calculator script that allows the user to submit two numbers and choose
an operation to perform on them (addition, multiplication, division, subtraction).
Use hidden fields with the script you created in activity 1 to store and display the
number of requests that the user has submitted.*/

\$sum = array("add\"=>(\$fnum + \$snum), // stores calculations
\"subtract\"=>(\$fnum-\$snum),
\"multiply\"=>(\$fnum*\$snum),
\"divide\"=>(\$fnum/\$snum));

\$submissions = (isset(\$submissions))?++\$submissions:0; // keeps track of how many times user has clicked calculate

if ( (!isset(\$fnum)) || (!isset(\$snum)) ) // if nothing is set for both number values print welcome message.
{
\$message = \"Welcome to the calculation script!\";
}

if ( (!isset(\$fnum)) && (isset(\$snum)) )
{
\$message = \"Value is missing in the first number field.\";
}

if ( (isset(\$fnum)) && (!isset(\$snum)) )
{
\$message = \"Value is missing in the second number field.\";
}

if ( (isset(\$fnum)) && (isset(\$snum)) ) // if both first and second number have a value
{
{
}

if (\$operation[\"subtraction\"]==\"subtraction\")
{
\$message = \$num[\"subtract\"];
}

if (\$operation[\"multiplication\"]==\"multiplication\")
{
\$message = \$num[\"multiply\"];
}

if (\$operation[\"division\"]==\"division\")
{
\$message = \$num[\"divide\"];
}
}

?>
<html>
<title>Testing Page</title>
<body>

<p><b><?php print \$message?></b></p>
<p> </p>
<p>You have used the calculator <?php print \$submissions?> times.</p>

<form action=\"<?php print \$PHP_SELF?>\" method=\"POST\">
<p>First Number: <input type=\"text\" name=\"fnum\" value=\"<?php print \$fnum?>\"></p>
<p>Second Number: <input type=\"text\" name=\"snum\" value=\"<?php print \$snum?>\"></p>
<p> </p>
<select name=\"operation[]\">
<option>subtraction
<option>multiplication
<option>division
</select>
<p> </p>
<input type=\"hidden\" name=\"submissions\" value=\"<?php print \$submissions?>\">
<p><input type=\"submit\" value=\"calculate!\"></p>
</form>
</body>
</html>

[b]But it doesn't work.[/b]

##### Share on other sites
I sent this to another post earlier maybe it will help. at
under the mathimatical chapters, there is a lot there that might help you figure those answers out.

##### Share on other sites
Please tell us the errors you are receiving. Also, I see that you are not setting your variables to the \$_POST data before trying to use it. Please set \$fnum = \$_POST['fnum']; etc... Unfortunately, since this is for school I'm not going to rewrite this script, however, I suggest you check out your book. (I'm a bit surprised, though, how you are using ternary operators without understanding to set \$_POST data...unless of course you are used to working on servers with register_globals on)