Valhalla Legends Forums Archive | Web Development | Calculator (PHP)

AuthorMessageTime
Corrupt
Hello, Today I tried to right my frist PHP script a calculator, the adding workings but nothing else does. :-/ this is my code.

HTML FORM

<html>
<head>
<title>Calculation Form</title>
</head>
<body>
<FORM method="POST" ACTION="calculate.php">
<p>Value 1: <INPUT TYPE="text" NAME="val1" SIZE=10></P>
<p>Value 2: <INPUT TYPE="text" NAME="val2" SIZE=10></p>
<P>Calculation:<br>
<INPUT TYPE="radio" NAME="calc" VALUE="add"> add<br>
<INPUT TYPE="radio" NAME="calc" VALUE="subtract"> subtract<br>
<INPUT TYPE="radio" NAME="calc" VALUE="multiply"> multiply<br>
<INPUT TYPE="radio" NAME="calc" VALUE="divide"> divide</P>
<P><INPUT TYPE="submit" NAME="submit" VALUE=Calculate"></P>
</FORM>
</BODY>
</HTML>


This is my PHP script:

<?
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] =="")) {
header("location: http://www.korrupted.org/calculate_form.html");
exit;
}
if ($_POST[calc] == "add") {
      $result = $_POST[val1] + $_POST[val2];
} else if ($POST[calc] == "subtract") {
      $result = $_POST[val1] - $_POST[val2];
} else if ($POST[calc] == "multiply") {
      $result = $_POST[val1] * $_POST[val2];
} else if ($POST[calc] == "divide") {
      $result = $_POST[val1] / $_POST[val2];
}
?>
<HTML>
<HEAD>
<TITLE>Calculation Results</Title>
</HEAD>
<BODY>
<P>The result of the calculation is: <? echo "$result"; ?></P>
</BODY>
</HTML>
May 6, 2005, 2:00 AM
St0rm.iD
that shouldnt work at all...you want quotes around the itmes in [].
May 6, 2005, 2:09 AM
Corrupt
Really? I'm reading a "beginners book" and they pretty much give you the code :-/ hrm let me try that.


EDIT: Yea I tried that just now still didn't work :-/ like add works but the other ones just show The result of the calculation is: and no number :-/
May 6, 2005, 2:32 AM
JoeTheOdd
You should use [.code][/.code] blocks. No dots. Doesn't PHP have a Val() function?
May 6, 2005, 2:56 AM
Corrupt
after talking with warrior on aim lol, I was doing $POST insead of $_POST lol :-/ this is new code for the PHP script

<?
if (($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) {
header("location: http://www.korrupted.org/calculate_form.html");
exit;
}
if ($_POST['calc'] == "add") {
      $result = $_POST['val1'] + $_POST['val2'];
} else if ($_POST['calc'] == "subtract") {
      $result = $_POST['val1'] - $_POST['val2'];
} else if ($_POST['calc'] == "multiply") {
      $result = $_POST['val1'] * $_POST['val2'];
} else if ($_POST[calc] == "divide") {
      $result = $_POST['val1'] / $_POST['val2'];
}
?>
<HTML>
<HEAD>
<TITLE>Calculation Results</Title>
</HEAD>
<BODY>
<P>The result of the calculation is: <? echo "$result"; ?></P>
</BODY>
</HTML>
May 6, 2005, 3:05 AM
Quarantine
[quote]
Warrior R 1337: I found your error
Warrior R 1337: You're doing
Warrior R 1337: $POST
Warrior R 1337: for everything else
Warrior R 1337: it should be
Warrior R 1337: $_POST
[/quote]
May 6, 2005, 3:16 AM
KoRRuPT
I'm not sure if it matters now, but I fixed your code and uploaded it for example. http://www.bunkmedia.net/public_img/calc

PHP Code
[php]
<?PHP
if (($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) {
header("location: http://www.bunkmedia.net/public_img/calc/index.html");
exit;
}
if ($_POST['calc'] == "add") {
$result = $_POST['val1'] + $_POST['val2'];
  } else if ($_POST['calc'] == "subtract") {
$result = $_POST['val1'] - $_POST['val2'];
  } else if ($_POST['calc'] == "multiply") {
$result = $_POST['val1'] * $_POST['val2'];
  } else if ($_POST['calc'] == "divide") {
$result = $_POST['val1'] / $_POST['val2'];
  }
?>
[/php]
May 6, 2005, 3:30 AM
raylu
This can and should be done in JS...

But anywa, how about this:
[code]<form method=post action=thephppage.php>
<input type=text name=calc><input type=submit>[/code]
thephppage.php:
[code]echo eval($_POST['calc'])[/code]

Although that does leave a huge security hole, it would be more practical as you can now type in stuff like 1+2/3*4
June 1, 2005, 11:16 PM
R.a.B.B.i.T
He wants to learn PHP, yet you give him code without explaining it and say to use JS.  Please go die.
June 2, 2005, 3:01 AM
KkBlazekK
I don't see any JS rabbit...
June 2, 2005, 3:20 AM

Search