Page 1 of 1

HELP: I can't locate the error....

Posted: Thu Nov 23, 2006 9:10 pm
by K82
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>
[/syntax]

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>
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]

[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]

Posted: Thu Nov 23, 2006 9:13 pm
by John Cartwright
what is the error aside from notices you'll be getting for not quoting your array indices.

Code: Select all

$_POST[val1] 
  // vs
$_POST['val1']
I recommend when developing scripts set your error reporting to E_ALL, via

Code: Select all

error_reporting(E_ALL);

Posted: Thu Nov 23, 2006 9:17 pm
by feyd
You've misspelled the VALUE attribute on the tag for multiply.

Posted: Thu Nov 23, 2006 9:24 pm
by K82
Why do I need to put single quote around arrays, they dont put them in the book. I copied the code right from the book. :?

I changed the value keyword on the html page and it works fine now. I thought the error was in the php file so I couldnt find it.

Posted: Thu Nov 23, 2006 10:02 pm
by John Cartwright
K82 wrote:Why do I need to put single quote around arrays, they dont put them in the book. I copied the code right from the book. :?
Not all books support good coding practices. Turn on error reporting, you'll see a bunch of notices.

Posted: Thu Nov 23, 2006 10:33 pm
by K82
I wouldn't doubt it, I've had nothing but trouble with this book since I started reading it. I really want to learn PHP but this is why I like Microsoft languages, at least I am fimilar with how to use them.

PHP seems like a cool language, but I will have to learn all of those little ins and outs of another programming language again.

Can you recommend a book for me to use, I may take this one back and get a different one but I don't know which one will show me the best ways.