Simple Calculator PHP Need Help!

Status
Not open for further replies.

dquigley

New Member
Messages
249
Reaction score
0
Points
0
I am creating a script from a book that is teaching me php and I am having an issue with this calculator script, it apears to be working but if I click submit I get the error
Forbidden

You don't have permission to access /calculate.php" on this server.
The first html script is

<HTML>
<HEAD>
<TITLE>Calculation Form</TITLE>
</HEAD>
<BODY>

<FORM METHOD="post" ACTION=calculate.php">
<P>Value 1: <INPUT TYPE="text" NAME="va11" SIZE=10></P>
<P>Value 2: <INPUT TYPE="text" NAME="va12" 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>
And the second script the php part is

<?

if (($_POST[va11] =="") || ($_POST[va12] =="") || ($_POST[calc] =="")) {
header ("Location: calculate_form.html");
exit;
}
if ($_POST[calc] == "add") {
$result = $_POST[va11] + $_POST[va12];
} else if ($_POST[calc] == "subtract") {
$result = $_POST[va11] - $_POST[va12];
} else if ($_POST[calc] == "multiply") {
$result = $_POST[va11] * $_POST[va12];
} else if ($_POST[calc] == "divide") {
$result = $_POST[va11] / $_POST[va12];
}

?>
<HTML>
<HEAD>
<TITLE>Calculation Result</TITLE>
</HEAD>
<BODY>

<P>The result of the calculation is: <? echo "$result"; ?></P>

</BODY>
</HTML>
Edit:
Sorry, nevermind after looking at my post for a minute I found the error
Ill post it in case any of you are trying to learn too or are curious.

Heres the error from the first html script
<FORM METHOD="post" ACTION=calculate.php">

Heres how I fixed it
<FORM METHOD="post" ACTION="calculate.php">
 
Last edited:

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
"$_POST[va11]" etc.. The va1x should be wrapped up in single quotes also, since they are strings. Ints dont need to be.
 
Status
Not open for further replies.
Top