/    Sign up×
Community /Pin to ProfileBookmark

Help with online calculator

I’m new to PHP, and coding in general. I’m trying to make an online calculator but I can’t get it to “calculate” the numbers. here are my scripts.

this is the form script

“Type in the numbers and the operations you wish to perform”
<form action=”answer.php” method=”REQUEST”>

# = <input type=”text” name=”1″ /><br>

operation = <input type=”text” name=”operation1″ /><br>

# = <input type=”text” name=”2″ /><br>

operation = <input type=”text” name=”operation2″ /><br>

# = <input type=”text” name=”3″ /><br>

operation = <input type=”text” name=”operation3″ /><br>

# = <input type=”text” name=”4″ /><br>

operation = <input type=”text” name=”operation4″ /><br>

# = <input type=”text” name=”5″ /><br>

<input type=”submit” value=”Calculate”><br>
</form>

That seems to work at the moment.

Now this script is the answer page.

<?php echo $_REQUEST[“1”]; ?>
<?php echo $_
REQUEST[“operation1”]; ?>
<?php echo $_REQUEST[“2”]; ?>
<?php echo $_
REQUEST[“operation2”]; ?>
<?php echo $_REQUEST[“3”]; ?>
<?php echo $_
REQUEST[“operation3”]; ?>
<?php echo $_REQUEST[“4”]; ?>
<?php echo $_
REQUEST[“operation4”]; ?>
<?php echo $_REQUEST[“5”]; ?>
= (insert code to calculate numbers here)

What do I have to do to make this work? The coding before the equal sign is there to show the problem, after the equal sign is where I want the answer to appear. REMEMBER, I’m only a few hours into this so if my script is COMPLETELY wrong, don’t laugh.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanOct 27.2004 — See attatched for a basic demo ?

[upl-file uuid=6ded62c7-537c-4009-8e1a-6a39d0398654 size=1kB]upload_1.txt[/upl-file]
Copy linkTweet thisAlerts:
@StrikerShot21authorOct 27.2004 — Hey! I was pretty close. So the code "eval" is what you use to evaluate?

I just tested it and I get this error

"Parse error: parse error in /home/www/strikershot21.hollosite.com/answer.php(11) : eval()'d code on line 1"

here is my script

<?php echo $_REQUEST["1"]; ?>

<?php echo $_
REQUEST["operation1"]; ?>

<?php echo $_REQUEST["2"]; ?>

<?php echo $_
REQUEST["operation2"]; ?>

<?php echo $_REQUEST["3"]; ?>

<?php echo $_
REQUEST["operation3"]; ?>

<?php echo $_REQUEST["4"]; ?>

<?php echo $_
REQUEST["operation4"]; ?>

<?php echo $_REQUEST["5"]; ?>

= <?php

$_
1 = eval($_REQUEST["1"]);

$_
1o = eval($_REQUEST["operation1"]);

$_
2 = eval($_REQUEST["2"]);

$_
2o = eval($_REQUEST["operation2"]);

$_
3 = eval($_REQUEST["3"]);

$_
3o = eval($_REQUEST["operation3"]);

$_
4 = eval($_REQUEST["4"]);

$_
4o = eval($_REQUEST["operation4"]);

$_
5 = eval($_REQUEST["5"]);

?>
Copy linkTweet thisAlerts:
@sciguyryanOct 27.2004 — That looks correct ?

RyanJ
Copy linkTweet thisAlerts:
@StrikerShot21authorOct 30.2004 — But why do I get the error?
Copy linkTweet thisAlerts:
@NogDogNov 01.2004 — Try this, perhaps?...
[code=php]
# create calculation string:
$calc = sprintf("%s %s %s %s %s %s %s %s %s",
$_REQUEST['1'],
$_REQUEST['operation1'],
$_REQUEST['2'],
$_REQUEST['operation2'],
$_REQUEST['3'],
$_REQUEST['operation3'],
$_REQUEST['4'],
$_REQUEST['operation4'],
$_REQUEST['5']);
# do eval (escape first $ because it is to be treated literally):
eval("$result = $calc;");
# display calc string and result:
echo "<p>$calc = $result</p>n";
[/code]
Copy linkTweet thisAlerts:
@StrikerShot21authorNov 06.2004 — That seems to work but what I don't understand is why I have to cancel the "$" in "$result"
×

Success!

Help @StrikerShot21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...