0% found this document useful (0 votes)
130 views

PHP Lesson Notes

This document contains PHP code for a basic calculator application with two scripts. The form_page.php script displays the calculator form and retrieves output from the post_page.php script. The post_page.php script performs the calculation based on the selected operator, and sends the result back to form_page.php. The document provides instructions on setting up a local development environment to run the PHP scripts, and recommends some ways to improve the code such as formatting the date output.

Uploaded by

api-336508272
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

PHP Lesson Notes

This document contains PHP code for a basic calculator application with two scripts. The form_page.php script displays the calculator form and retrieves output from the post_page.php script. The post_page.php script performs the calculation based on the selected operator, and sends the result back to form_page.php. The document provides instructions on setting up a local development environment to run the PHP scripts, and recommends some ways to improve the code such as formatting the date output.

Uploaded by

api-336508272
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP LESSON NOTES.

27th October 2016


There are two scripts in this document.
form_page.php (contains the form and visible output) and
post_page.php (the calculator as pure php) which must be saved in a
php_scripts folder (see below)
If you change any of these names or use uppercase where lower case is present,
the program will fail. PHP is case specific throughout, thus $firstName is not the
same as $FirstName, its the same generally with most coding languages
(generally speaking) and folder names. Avoid white space also,
Use camel case as a naming protocol if you like as in firstName which is easier
to read than firstname in a massive amount of text.
When you get the script running, keep a copy and be brave, mess with it there
could be improvements for instance the date formatted output is untidy, make
it better (research php date formats). Really brave people could try adding
another input to work with three numbers, or add some php in the post_page.php
file that checks that only integers have been submitted in the file.
Remember that you cant run this script without having access to a server
(virtual or otherwise) the reason is that PHP and MySQL runs on a server (server
side scripting language) and as such returns html back to the browser.

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");
?>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

You might also like