HELP: I can't locate the error....
Posted: Thu Nov 23, 2006 9:10 pm
feyd | Please use [/syntax]
Please tell me what is wrong with the code.
Thanks
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I am trying to do an example from a book I have and its driving me nutz, its a simple calculator just to test how PHP works with HTML forms. The calculator works fine except the multiply function always returns nothing for a result.
I have stared at if for an hour I can't find the error. I must have a stupid syntax error in there some place, but I can't find it.
[syntax="html"]<HTML>
<HEAD>
<TITLE>Calcualtion 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" VAULE="multiply"> multiply<br>
<INPUT TYPE="radio" NAME="calc" VALUE="divide"> divide</P>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Calculate"></P>
</FORM>
</BODY>
</HTML>Code: Select all
<?
if ( ($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "" ) ) {
header("Location: 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 Result</TITLE>
</HEAD>
<BODY>
<P>The result of the calculation is: <? echo "$result"; ?></P>
</BODY>
</HTML>Thanks
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]2.[/b] Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.[/quote]