PHP 6 Fast and Easy Book

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
oakbarn
Forum Newbie
Posts: 3
Joined: Wed Dec 10, 2008 9:53 am

PHP 6 Fast and Easy Book

Post by oakbarn »

I am trying to learn PHP using this book but cannot get past this lesson as it does not work.

I think that I have written to code as in the book but I get no results:

There is a HTML Form that appears as it should, but the call to the PHP does not seem to work correctly.

HTML Form

calculate_form.html
In line 6 it calls the php

1 <HTML>
2 <HEAD>
3 <TITLE> PHP Script</TITLE>
4 </HEAD>
5 <Body>
6 <FORM METHOD ="post" ACTION = "calculate.php">
7 <P> Value1: <INPUT TYPE = "text" NAME = "val1" SIZE = 10 \><P>
8 <P> Value2: <INPUT TYPE = "text" NAME = "val2" SIZE = 10 \><P>
9 <P> Calculation:<BR>
10 <P><INPUT TYPE = "radio" NAME = "calc" VALUE = "add"> add <BR>
11 <P><INPUT TYPE = "radio" NAME = "calc" VALUE = "subtract"> subtract <BR>
12 <P><INPUT TYPE = "radio" NAME = "calc" VALUE = "multiply"> multiply<BR>
13 <P><INPUT TYPE = "radio" NAME = "calc" VALUE = "divide"> divide </P>
14 <P><INPUT TYPE = "submit" NAME = "submit" VALUE = "Calculate"><P>
15 </BODY>
17 </HTML>
The php script:

calculate.php

1 <?PHP
2 if (($_POST[Val1] == ""|| $_POST[Val2] == ""||$_POST[Calc] == "")){
3 header("Location: calculate_form.html");
4 exit;
5 }
6 if ($_POST[calc] == "add") {
7 $result = $POST[val1] + $_POST[val2];
8 } else if ($_POST[calc] == "subtract") {
9 $result = $POST[val1] - $_POST[val2];
10 } else if ($_POST[calc] == "multiply") {
11 $result = $POST[val1] * $_POST[val2];
12 } else if ($_POST[calc] == "divide") {
13 $result = $POST[val1] / $_POST[val2];
14 }
15 ?>
16 <HTML>
17 <HEAD>
18 <TITLE> Calculation Result</TITLE>
19 </HEAD>
20 <BODY>
21 <P> The result of the calculation is: <?PHP echo "$result";?></P>
22 </BODY>
23 </HTML>




When I debug the code in PHP Designer, I get no output.


When I load the form and fill in the values and a radio button, it simple clears the form and does not give line 21

I would also like to echo a statement in the code so I can get errors:

For example:

In Line 3 above of the calculate_form.html I would like to echo that "You need to add values or click a radio button" . This would also help me find errors in my code. Right now, I am not sure if it even executes the calculate.php.

I know that I am missing something simple but I am trying to stop using VB and go to PHP and mysql. :banghead: :banghead:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP 6 Fast and Easy Book

Post by requinix »

You got that code from a book? So I can not recommend it to people, what book is it?

Code: Select all

if (($_POST[Val1] == ""|| $_POST[Val2] == ""||$_POST[Calc] == "")){
PHP is case-sensitive about a lot of stuff: form fields are one of them. Your form calls them "val1", "val2", and "calc", but the PHP code is looking for "Val1", "Val2", and "Calc". The capitalization matters.

Change those to lowercase and it should work fine.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: PHP 6 Fast and Easy Book

Post by watson516 »

You also seem to be missing a </form> in your html file.
oakbarn
Forum Newbie
Posts: 3
Joined: Wed Dec 10, 2008 9:53 am

Re: PHP 6 Fast and Easy Book

Post by oakbarn »

Thanks for the help.

I should have checked my syntax better. I found several mistakes and will get better as I learn.

I have yet to find out how to post code with the numbers on this site. I have searched and see the code button at the top but all is does is
Is it a file you upload (as a txt file becuase it will not accept .php?

The book is PHP 6 Fast and Easy Web Development by Julie Meloni and Matt Telles. I cannot find a support web site for the book or I would have gone there.

:o I am still having one problem with the header being re loaded, but I suspect that I have some white space that needs to be removed.
Post Reply