Valhalla Legends Archive

Programming => Web Development => Topic started by: Corrupt on May 05, 2005, 09:00 PM

Title: Calculator (PHP)
Post by: Corrupt on May 05, 2005, 09:00 PM
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>
Title: Re: Calculator (PHP)
Post by: Banana fanna fo fanna on May 05, 2005, 09:09 PM
that shouldnt work at all...you want quotes around the itmes in [].
Title: Re: Calculator (PHP)
Post by: Corrupt on May 05, 2005, 09:32 PM
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 :-/
Title: Re: Calculator (PHP)
Post by: Joe[x86] on May 05, 2005, 09:56 PM
You should use [.code][/.code] blocks. No dots. Doesn't PHP have a Val() function?
Title: Re: Calculator (PHP)
Post by: Corrupt on May 05, 2005, 10:05 PM
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>
Title: Re: Calculator (PHP)
Post by: Warrior on May 05, 2005, 10:16 PM
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
Title: Re: Calculator (PHP)
Post by: Akamas on May 05, 2005, 10:30 PM
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
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'];
  }
?>
Title: Re: Calculator (PHP)
Post by: raylu on June 01, 2005, 06:16 PM
This can and should be done in JS...

But anywa, how about this:
<form method=post action=thephppage.php>
<input type=text name=calc><input type=submit>

thephppage.php:
echo eval($_POST['calc'])

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
Title: Re: Calculator (PHP)
Post by: R.a.B.B.i.T on June 01, 2005, 10:01 PM
He wants to learn PHP, yet you give him code without explaining it and say to use JS.  Please go die.
Title: Re: Calculator (PHP)
Post by: Blaze on June 01, 2005, 10:20 PM
I don't see any JS rabbit...