PHP 6 Fast and Easy Book
Posted: Wed Dec 10, 2008 10:17 am
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.

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.