PHP Lesson Notes
PHP Lesson Notes
If you do load a virtual server on your machine you should see something like
this;
C:// xammp folder ----- htdocs folder
And you will have to save your files as another folder (named after your project
page) in the htdocs folder; thus
Now for the scripts. Copy only the text between the >>>>>>>>>>>>
symbols
Form_page.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<?php
// everything that is preceded by double forward slash is a comment in php and
will be ignored
// if you remove the slashes php will try to process the string within and break.
// this if / else script checks if there is a message returned from the post page
header
// and if yes, sets the $message variable to whatever the page header sent
// if there isn't (else) then the 'message' should read "try me",
// note, the @ before the post request suppresses the error thrown up (in some
ide's) when the
// 'message' post data var is not defined (empty), it's not 100% needed but worth
remembering.
if ( @$_REQUEST['message'] ) {
$message = ($_REQUEST['message']);
} else {
$message = "try me";
}
// capture the 'total' header variable from the post page header and save to
$total variable
$total = @$_REQUEST['total'];
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP calculator</title>
</head>
<body>
<form action="php_scripts/post_page.php" method="post"
enctype="multipart/form-data" name="form1" id="form1">
<table width="400" border="1" align="center" cellpadding="4"
cellspacing="4">
<tbody>
<tr>
<td align="right">Calculator date</td>
<td align="center"><?php echo date('D- M - Y'); ?></td>
</tr>
<tr>
<td align="right">sum one</td>
<td><input type="text" name="sum_1" id="sum_1"></td>
</tr>
<tr>
<td align="right">operator</td>
<td align="left"><select name="operatorSelect" id="operatorSelect">
<option value="multiply">multiply</option>
<option value="divide">divide</option>
<option value="plus" selected="selected">plus</option>
<option value="minus">minus</option>
</select></td>
</tr>
<tr>
<td align="right">sum two</td>
<td><input type="text" name="sum_2" id="sum_2"></td>
</tr>
<tr>
<td align="right">total</td>
<td><input type="text" name="total" id="total" value="<?php echo
$total; ?>"></td>
</tr>
<tr>
<td align="right"><?php echo $message; ?></td>
<td align="right"><input type="submit" name="calculate" id="calculate"
value="Submit"></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Next the post_page.php
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<?php
// capture the post data to get values then save the values to variables
$sum_1 = $_REQUEST['sum_1'];
$sum_2 = $_REQUEST['sum_2'];
// capture the post data to ascertain the correct operator to place in the
calculation
$operator = $_REQUEST['operatorSelect'];
// we sent the operator as a string so we use a 'switch' function to get assign the
text to the symbol
// the operator variable is passed into the switch function as an argument and is
tested. If the test is
// met the do the maths and then the 'break' command does not allow any other
results to be considered.
// think of this as a shorter way than if / else script. Better for large comparisons
switch ($operator) {
case "plus":
$total = $sum_1 + $sum_2;
break;
case "minus":
$total = $sum_1 - $sum_2;
break;
case "multiply":
$total = $sum_1 * $sum_2;
break;
case "divide":
$total = $sum_1 / $sum_2;
break;
}
// now we have the correct operator send the result to the post page.
// note that as the header is a string a (simple) solution is to end the string with "
and then concat
// the total variable (.) and then restart the string with "
// notice also the composition of the header string. As the destination 'form page
is not in the same
// folder we add ../ to choose the root where the page resides
header("location:../form_page.php?total=".$total."&message=calculated");
?>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>