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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
K82
Forum Newbie
Posts: 14
Joined: Mon Nov 20, 2006 5:27 pm
Location: Mid-West

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

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You've misspelled the VALUE attribute on the tag for multiply.
User avatar
K82
Forum Newbie
Posts: 14
Joined: Mon Nov 20, 2006 5:27 pm
Location: Mid-West

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
K82
Forum Newbie
Posts: 14
Joined: Mon Nov 20, 2006 5:27 pm
Location: Mid-West

Post 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.
Post Reply