Jump to content

calculator script


olivia

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[\"addition\"]==\"addition\")
{
$message = $sum[\"add\"];
}

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

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

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

?>
<html>
<head>
<title>Testing Page</title>
<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.mavurik.net/content/layout/css/mavurik.css\">
<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>
<p>Please select an operation method:</p>
<select name=\"operation[]\">
<option>addition
<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]
help me please.
Link to comment
Share on other sites

I sent this to another post earlier maybe it will help. at
[a href=\"http://www.hudzilla.org/phpbook/read.php/3_1_0\" target=\"_blank\"]http://www.hudzilla.org/phpbook/read.php/3_1_0[/a]
under the mathimatical chapters, there is a lot there that might help you figure those answers out.
Link to comment
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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.