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>
that shouldnt work at all...you want quotes around the itmes in [].
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 :-/
You should use [.code][/.code] blocks. No dots. Doesn't PHP have a Val() function?
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>
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
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'];
}
?>
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
He wants to learn PHP, yet you give him code without explaining it and say to use JS. Please go die.
I don't see any JS rabbit...