• Welcome to Valhalla Legends Archive.
 

Calculator (PHP)

Started by Corrupt, May 05, 2005, 09:00 PM

Previous topic - Next topic

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>

Banana fanna fo fanna

that shouldnt work at all...you want quotes around the itmes in [].

Corrupt

#2
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 :-/

Joe[x86]

You should use [.code][/.code] blocks. No dots. Doesn't PHP have a Val() function?
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Corrupt

#4
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>

Warrior

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 from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Akamas

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
<?PHPif (($_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'];   } ?>?>
Quote from: Arta[vL] on August 14, 2006, 04:57 PM
Well, I want some too. Greedy Yoni should stop hogging it.

raylu

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
Pie?

R.a.B.B.i.T

#8
He wants to learn PHP, yet you give him code without explaining it and say to use JS.  Please go die.

Blaze

I don't see any JS rabbit...
Quote
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No