JavaScript Text, Appendix B (Handling Form Submission, Working with Files), pgs 668 - 676
W3 Schools PHP Tutorial
PHP - File Write Tutorial
PHP - File Read (and gets) tutorial
Practice:
- PHP Practice
-
You will continue to work with your money exchange program.
You are going to update your ATM program that not only
allows you to have a money exchange, but also checks your balance and will later makes deposits and withdrawals.
To do this you will need to create a file that keeps track of your balance. Which means you must
use a server-side script to access the file.
- Create an assignment4 folder on your hard drive.
- You are to first create a file that will hold your balance. You are to start with 1000.00.
- Open your editor.
Enter 1000.00.
Save the file as balance.txt in the assignment4 folder.
- Using SSH (or whatever file transfer program that you use), upload the assignment4 folder.
- From SSH, right click on the balance.txt file. Click on properties.
Click on each check box in the Read and Write columns or simply enter 666. You should see 666 in the
permission code. Click OK. (If you use another file transfer program your goal is to end
up with "chmod 666 for your balance.txt"
- Every time you reupload this file, you will need to go back and set the permission code.
- Since you are to have multiple options, you need to give the user several buttons to choose from.
This means that you will not set the action in the form, you will use a button type instead of a submit
type, and you will need to build your own state information to take with you to the next page.
- View pickTrans.htm
<h1>Testing Selecting Transactions</h1>
<form id="atmButton" method="post" action="">
<p>Enter Name<br />
<input type="text" name="name" value="" /></p>
<p>
<input type="button" id="bal" value="Balance"
onclick="location.href = 'testBalance.php' + '?' + 'name='+ document.forms[0].name.value;" />
<input type="button" id="depos" value="Deposit"
onclick="location.href = 'testDeposit.php' + '?' + 'name='+ document.forms[0].name.value; " />
</p>
</form>
- Notice the use of the location.href command to link to another page.
- Practice using the pickTrans.htm page and notice the URL shows the state information.
- Open your login page from assignment3 and save to your assignment4 folder.
- Change your submit button for your Money Conversion to be of type button and to use
the onclick event handler to create a location.href with state information.
- Add buttons to your login page to have buttons to withdraw, check balance,
deposit, and money conversion.
- Change your form tag to a null action.
- Upload your new login page to your assignment4 folder and test to see that the Money Conversion button
still works the same as it did in assignment3.
- Reading the balance from your file
- Create a testBalance.php page that calls a php file to read the balance.
- Create a getBalance.php page.
- Making a deposit that updates your balance in your balance.txt file
- Create a testDeposit.php page
<?php
include 'getBalance.php';
$returnValue = readBalance();
echo "<p>Starting balance: " ;
printf ( "%01.2f", $returnValue ) ;
echo "</p>" ;
?>
<h1>Testing Deposit Page</h1>
<form id="deposit" name ="deposit" method="post"
action="addBalance.php" enctype="application/x-www-form-urlencoded">
<p>deposit amount:<input type="text" name= "depositAmount" id="depositAmount" /></p>
<p><input type="submit" id="makeDeposit" value="Make Deposit" /></p>
</form>
- Look at the code.
- The php coding to show the starting balance is the same as the coding used in testBalance.php.
- The form tag shows that the action transfers the page to addBalance.php.
- There is a text box to obtain a deposit amount.
- Create a addBalance.php page
<?php
$newDeposit=$_POST['depositAmount'];
$file = "balance.txt";
$fp = fopen($file, "r");
if (!$fp)
{
echo "<p>Could not open the data file.</p>";
echo "The system is down. Please try back later.";
echo "<p><a href=\"login.php\">Login Page</a></p>";
exit;
}
else
{
$balance = fgets($fp);
}
fclose ($fp);
$file = "balance.txt";
$fp = fopen($file, "w+");
if (!$fp)
{
echo "<p>Could not open the data file.</p>";
echo "The system is down. Please try back later.";
echo "<p><a href=\"login.php\">Login Page</a></p>";
exit;
}
else
{
$newBalance = $newDeposit + $balance;
fwrite( $fp, "$newBalance");
}
fclose ($fp);
echo "Thank you for your deposit.<br />For another transaction return to the login page.";
echo "<p><a href=\"login.php\">Login Page</a></p>";
?>
- $_GET and $_POST variables are used to retrieve information from forms, like user input.
In the first statement $_POST looks for the value associated with the name depositAmount and
assigns that value to the variable $newDeposit.
- This coding is similar to the coding in getbalance.php; however, there are a few differences.
- When the file does not open:
- a message is sent "Could not open the data file"
- a message is sent to the user that "The system is down. Please try back later."
(If your properties of your balance.txt are not set properly, you will see this
message.)
- a link back to the login page is provided.
- the code is exited so that the rest of the code cannot be invoked.
- When the file opens,
- the value written to the file is read by the fgets() function and that value is
assigned to the variable $balance.
- The file is again closed.
- You had to close the file because you want to write back to the file.
- Open the file again, but this time using a w+ so that you can overwrite the old
data with the new data.
- Again you test to see file was not opened, display appropriate messages, and exit if it did not.
- When the file opens correctly:
- assign the variable $newBalance to be the value of
$newDeposit added to $balance.
- write out the $newBalance to the file (this is an overwrite).
- Close the file
- Display messages to the screen.
- Add a link to allow the user to return to the Login Page.
Project for this assignment
- Update your pages to include links on your login page to check your balance, make a deposit,
make a withdrawal, or link to the money conversion page.
- Write an html page for your withdrawal page.
- Write a php page to subtract from your deposit. On your withdrawal page, you must make sure that
the balance is greater than the withdrawal amount. If it is not, you cannot make the withdrawal.
Update your index page:
- Add links on your index page to the pages in your assignment4 folder that are to be graded:
Uploading:
- Upload your revised index.htm page to the public_html folder.
- Upload your assignment4 folder with the files:
- login.php
- conversion.htm
- currency.htm
- testBalance.php
- testDeposit.php
- testWithdrawal.php
- getBalance.php
- addBalance.php
- subBalance.php
Testing uploaded pages
- Follow the checklist for grading shown on
the schedule page.
- Make any corrections for any page that does not validate or display correctly.