Calculator help

Hey! I’m a new student when it comes to php development… I am really stumped on a number of my assignments but this one in particular seems simple enough and yet I can’t seem to figure out what I’m doing wrong. Help. I’m supposed to make a php calculator using the $_GET to retreive the variable for the index.html… but nothing is coming up when i tested it.

[php]

Calculator <?php $integer1 = $_GET["value1"]; $integer2 = $_GET["value2"]; $operand = $_GET["opt"]; $value1 = (int)$integer1; $value2 = (int)$integer2; if (!$value1) { print "Please enter a number in Interger 1."; } ?> [/php]

[code]

Untitled 2 Calculator
Integer 1
+ - * /
Integer 2
Solution:
[/code]

please help!!

Is the page that you’re requesting a PHP page?

You’re also not doing anything with the variables once you calculate them. From the looks of your form, a switch statement might work nicely.

the first code is the php page! and thanks so much for responding to me! here is the new code i have so far…
this is the php the html is in the first post

[php]

Calculator <?php $int1 = $_GET["value1"]; $int2 = $_GET["value2"]; $operand = $_GET["opt"]; $submit= $_GET["submit"]; $value1 = (int)$integer1; $value2 = (int)$integer2; if ($operand == '+') { $value1 + $value2 = $solution; echo "$solution"; } elseif ($operand == '-') { $value1 - $value2 = $solution; echo "$solution"; } elseif ($operand == '*') { $value1 * $value2 = $solution; echo "$solution"; } elseif ($operand == '/') { $value1 / $value2 = $solution; echo "$solution"; ?>

?>

[/php]

Okay here is the code I’ve been working on… if i load this in a browser shouldn’t i be able to see the result? cause i can’t

[php]<?php

$num1 = $_GET[‘num1’];
$num2 = $_GET[‘num2’];
$cal = $_GET[‘cal’];

switch($cal) {

case '+':
echo $num1+$num2;
break;

case '-':
echo $num1-$num2;
break;

case '*':
echo $num1*$num2;
break;

case '/':
echo $num1/$num2;
break;

default:
echo "Invalid Operator";

}

?>[/php]

[code]

Untitled Document

Number 1:


Number 2:


Operator


[/code]

I think the GET method might be messing with it.

Change form method to POST and all the $_GET to $_POST and try that.

i wish i could use POST!! my prof. hinted that he prefered GET for this assignment tho… However I did try what you say and still no go… do I have to upload it to a server for the PHP to return my calculations? Here is the updated code so far.

[php]

Untitled Document <?php $num1 = $_GET['num1']; $num2 = $_GET['num2']; $cal = $_GET['cal']; switch($cal) { case '+': echo $num1+$num2; break; case '-': echo $num1-$num2; break; case '*': echo $num1*$num2; break; case '/': echo $num1/$num2; break; default: echo "Invalid Operator"; } ?> [/php]

The only thing I could think of is that when you’re putting the operator in the URL, your browser does not like the character or thinks it’s there for another reason. Try using ‘add’, ‘sub’, ‘mul’, and ‘div’ instead of your character representations.

Also, if this is for an assignment, I would get into the practise of checking if your user-sent variables are set first. The isset function will check that they have been entered - otherwise you will get a notice when you try to use them (when they’re not provided). You could also go one step further and add validation. Not only could you validate the user input, you could also check to make sure that you can calculate with it - for example, they have not trying to divide by 0.

Sponsor our Newsletter | Privacy Policy | Terms of Service