I am new to the world of coding as well as XHTML and PHP. As a way to gain more experience as well as to put theory into practice including a way to challenge myself, I put together the following code that calculates values a user enters into a form.
This was to test my knowledge regarding PHP's own functions but also creating custom functions. I am sure the code can be improved and would appreciate any advise.
The one thing I was unable to do was to only display the result and to hide the form once it has been posted preferably on the same page i.e. not redirecting the user to another page.
I have also tried to keep business logic and presentation as much as possible.
Appreciate any advise as well as constructive criticism.
Main Page Code - Calculator.php
<?php
require_once('includes/base.php');
require_once('functions.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Calculator</title>
</head>
<body>
<h1>Calculator Instructions</h1>
<p>Below are instructions on using the calculator</p>
<ol>
<li>You must specify 2 values in the fields below</li>
<li>You must select an arithmetic operator which includes the list below
<ul>
<li>Multiply</li>
<li>Divide</li>
<li>Modulus</li>
<li>Add</li>
<li>Subtract</li>
</ul>
</li>
<li>Simply submit the form once you have specified 2 values and selected arithmetic operator</li>
</ol>
<h2>Calculator</h2>
<div>
<?php
if(isset($_POST['submit'])) {
echo mycalculator();
}
?>
</div>
<form name="calculator" method="post" action="calculator.php">
<div>
Field 1: <br />
<input type="input" name="field1" value="<?php if(isset($_POST['field1'])) { echo $_POST['field1']; } ?>" />
</div>
<div>
Field 2: <br />
<input type="input" name="field2" value="<?php if(isset($_POST['field2'])) { echo $_POST['field2']; } ?>" />
</div>
<div>
Select an arithmetic operator: <br />
<select name="field3">
<option value="">Please select an option</option>
<?php
echo myselect();
?>
</select>
</div>
<div>
</div>
<div>
<input type="submit" name="submit" value="submit" />
<input type="reset" name="reset" value="reset" />
</div>
</form>
</body>
</html>
Define and Set directory and path - Base.php
<?php
//Retrieve parent directory
$parentdirectory = dirname(dirname(__FILE__));
$unixpath = str_replace('\\', '/', $parentdirectory);
//Define root directory
define('BASE_DIRECTORY', $parentdirectory);
//Set includes directory
ini_set('include_path', $parentdirectory.'\includes');
?>
Business Logic - Functions.php
<?php
//Generate arithmetic menu
function myselect () {
//Define arithmetic operators
$selectarray = array('multiply', 'divide', 'modulus', 'add', 'subtract');
$status = '';
foreach($selectarray as $seletkey => $selectvalue) {
if($_POST['field3'] == $selectvalue) {
$status = 'selected';
}
else {
$status = '';
}
echo '<option value="'.$selectvalue.'" '.$status.'>'.$selectvalue.'</option>';
}
}
//Validate if form has been submitted
if(isset($_POST['submit'])) {
//Generate errors if form is not completed
function mycalculator() {
//Instatiate form fields, error messages and arithmetic operators
$formfields = array('a' => 'field1', 'b' => 'field2', 'c' => 'field3');
// $formfieldkeys = array_values($formfields);
$formfieldkeys = array_combine(range(1, count($formfields)), array_values($formfields));
//Instantiate error messages array
$errormsg = array();
//Validate if form field has been set
foreach($formfieldkeys as $formfieldkey => $formfieldvalue) {
$formfield = isset($_POST[$formfieldvalue]) ? $_POST[$formfieldvalue] : '';
//Return error message
if(empty($formfield)) {
$errormsg = 'Please specify a value for field '.$formfieldkey.'<br />';
/* Return error message for the specific field concerned using array_values
$errormsg = 'Please specify a value for field '.($formfieldkey + 1).'<br />';
*/
echo $errormsg;
}
//Validate if the values being posted for fields 1 - 2 are numeric
if(in_array($formfieldvalue, array('field1','field2'))) {
if(!is_numeric($formfield)) {
$errormsg = 'You must provide a numeric value for field '.$formfieldkey.'<br />';
/* Return error message for the specific field concerned using array_values
$errormsg = 'You must provide a numeric value for field '.($formfieldkey + 1).'<br />';
*/
echo $errormsg;
}
}
if(!empty($formfield)) {
//Extract individual array values
// extract($formfields);
// $field1 = $_POST[$a];
// $field2 = $_POST[$b];
// $field3 = $_POST[$c];
//Dynamic Variables
${"field{$formfieldkey}"} = $_POST[$formfieldvalue];
if(!empty($field1) && !empty($field2) && !empty($field3)) {
if($field3 == 'multiply') {
$calculate = $field1 * $field2;
// echo $calculate;
}
elseif($field3 == 'divide') {
$calculate = $field1 / $field2;
// echo $calculate;
}
elseif($field3 == 'modulus') {
$calculate = $field1 % $field2;
// echo $calculate;
}
elseif($field3 == 'add') {
$calculate = $field1 + $field2;
// echo $calculate;
}
elseif($field3 == 'subtract') {
$calculate = $field1 - $field2;
// echo $calculate;
}
else {
$calculate = 'error';
}
if($calculate > 0) {
return 'The answer is '.$calculate;
}
}
}
}
}
//Debug
// print_r($_POST);
}
?>