Building a Banking Application Home Page with OOP / Page 2 | WebReference

Building a Banking Application Home Page with OOP / Page 2


Building a Banking Application Home Page with OOP [con't]

When the user clicks on the withdraw link on the home page, the link brings the customer to the withdraw script. This script is responsible for enabling a user to withdraw money from their chosen account. The page shows both the navigation panel and the account balance. On the home page, all of the accounts that are in the customer's name are shown. All that the customer needs to do is to select the account that he or she wants to use. In the picture below, the customer has selected the savings account, which has a balance of two hundred dollars:

See Figure 2

The script presents the user with an HTML form that has one field that will collect the amount that the user wants to withdraw. Below is the code that makes up the page:

The code has two parts to it, HTML and PHP. The PHP part is responsible for processing the data that it receives from the HTML part of the script. When the customer selects the withdraw option on the home page, some information is sent to the withdraw page. This information is then extensively used on the withdraw.php page. So lets take a look a the code, At the top of the script, a session is started:


Then all of the classes that are required are loaded:

include "class.acc.php";
include "class.cust.php";

We then instantiate the class that we want to use on this script. Since this script mostly deal with accounts and all the methods that deal with accounts are defined in the accounts class, we instantiate that class:

$obj = new Account; 

Then we set the error variables:

$err = FALSE; 
$errmsg = "";

Once the error variables are set, we then check to see if the form data has been submitted. It might be worth using another form field to test for the submission of the form, because sometimes users press the return key and not the submit button as we want them to. This way both scenarios are catered to:


Now because we are dealing with form data, we have to assume that it is faulty or that it is a security risk. Therefore, we need to validate the data. In our case, it is easy. We know that the amount that the customer entered should be of type numeric, so we simply check if the data is of type numeric using the is_numeric() function. In addition, we check to see if the value is empty or not. The form also sends two other pieces of information to the PHP code; it sends the account number and the current balance. Both these pieces of information come from the home page and can reasonably be assumed safe, so we don't really need to do any kind of validation on them:

As always, if there are no errors, then we continue with the rest of the code:


Now, we need to check if the current balance is greater than the amount that the customer wants to withdraw. So we compare the two amounts that are sent by the form:

if($_POST['currbal'] >

Then we subtract the amount that we want to withdraw from the current balance on the account:

//make the witdrawal $newbal = $_POST['currbal']
- $_POST['amount']; } 

We store the result of the subtraction in a variable called $newbal. The next step is to update the account balance in the database. This job is done by the setbalance()$method of the accounts class. We will discuss the method in detail shortly. We run the setbalance()method and store its result in a variable called $w:


The setbalance() method returns a TRUE or FALSE. We need to determine which of the two it is and then deal with them. We test for the result of the method:

//now update the
account balance

If the method returns successful, we send the customer to the withdrawdone.php page. This page shows the customer the outcome of the transaction:


If the setbalance() method returns negative, then it means that something went wrong in the database query, so we set an appropriate error message for the customer:

The HTML portion of the script also contains some PHP code, so let's take a look at it. First, we start with the HTML headers:

We define some styles for the header element:

The body section of the HTML table is where the main action takes place. The home page sends over the account number of the account that the customer selected. When it reaches the withdraw script, this account number is used to get the current balance of the account, as you will see shortly:

The navigation panel is then built:

Once the navigation panel is complete, the code continues to build the form that will display the balance, capture the account number and provide an input element for the customer to input the amount that they want to withdraw:

In the next row of the table, the balance is displayed. The balance itself is retrieved by a method of the accounts class called currbalance(). This method takes one parameter, which is the account number:


Here we run the currbalance() method and store its result in a variable called $currbal:

<?php $currbal = $obj->currbalance($_GET['accno']); 

Finally we show the contents of the $currbal variable:

echo $currbal; ?>

This balance is also stored in a hidden field called currbal. It will be submitted with the other form variables to the PHP code that we just discussed. Also stored in a hidden form field is the account number that is sent over from the homepage:

The next part of the form actually collects the amount that the customer wants to withdraw:

Finally, the HTML table and form elements are closed:

In the next article we will look at the two methods: currbalance() and setbalance(). We will also look at how the deposit.php script works. Until then, happy coding!

Original: November 4, 2009