Advanced Processing of User Data in PHP

The previous column, 'Collecting user data' looked at how to accept data from basic HTML forms and store then in a structured format in a file. As this is such an important topic for those looking to become skilled in PHP development, we will revisit it here. Over the past few columns we have been building up to a complete and useful project powered by PHP: a basic address book which can accept new data and search through existing data.

Python vs. PHP: Choosing your next project's language

Accepting New Address Book Data

In order to be useful, an address book must be able to store and receive address details from the address book user. Address Book 1 (AB1) will be able to receive the following data: first and last name of the addressee; the street number, street name, suburb, post code, state; the phone number (including area code) and mobile phone (if applicable); and, e-mail address (if applicable). As such, the HTML form looks as follows:

First name:
Last name:
Street Number:
Street Name:
Post code:
Area Code: Phone Number:
Mobile Phone (if applicable):
Email Address (if applicable):

This HTML code builds the complete interface for updating the address book. Note the FORM attributes on line 1: the form action is set to the name of the script (held in the variable $PHP_SELF) with PHP code. As such, the form data will be sent to the script and it will have to detect whether to show the HTML FORM or add new data. The following PHP code handles this condition as well as processing the input.

Also, notice that the mobile phone number and e-mail address are not mandatory values. As such, the script powering AB1 will have to be able to test if the mobile phone number and e-mail address are sent and store them in such a way as to allow a parser to detect their presence. Conversely, the script will also have to ensure that the mandatory values are sent.

If the HTML form is filled in the user clicks 'Add', then $submit will be set to 'Add'. The second line of the script checks for this condition. The script then builds a string from the data provided. Notice that either a one- of two-character identifier is prepended to the value and that each uploaded value is stored on its own line. This is done to simplify the work that needs to be performed by the parser: the value of a given line can be determined by reading the leading character (and the second character, if necessary).

The data is then stored in the file "address.dat". The script checks to make sure that all the data is written to the file before closing it. If the length written is less than the length of the string, an error is raised, since a short write will probably correct the address book and the parser will not be able to read it. This problem will be addressed in later months when we look at databases and PHP.

/* Add the user data */
if(isset($submit) && (strcmp($submit,"Add") == 0)) {
/* Build a string representing the data
* F = first name, L = last name, A1 = street number,
* A2 = street name, A3 = suburb, A4 = post code
* A5 = state P1 = area code, P2 = phone number,
* P3 = mobile number (if entered), E = email (if entered)
$data = "\nF$fn\nL$ln\nA1$snum\nA2$sname\nA3$sub\
nA4$pc\n" ."A5$state\nP1$ac\nP2$pn\n".(isset($mp) ?
"P3$mp\n" : "")
.(isset($e) ? "E$e\n" : "")."-\n";
$fp = fopen("address.dat","a+");
if(fwrite($fp,$data) == strlen($data)) {
echo "Successfully uploaded data\n";
} else {
echo "Write to address book was invalid. Data may
be corrupted!";

Verifying user data

There are nine mandatory fields for the AB1 interface but the script processing the uploaded data does not check that it has been sent by the user. To do this in PHP, the script must run the following code on each variable which is mandatory:

if(!isset($var)) {
$err[] = "var";

Where 'var' is the name of the variable being tested (such as 'fn' or 'sub'). If the variable is not set, it is registered in the array $err so that it can be processed elsewhere in the script, requesting the user to provide the information. For example:

if(count($err)) {
// there was missing data
if(in_array("sub",$err)) {
You must submit a suburb, please try again
// test other mandatory variables

The code is only executed if an error has been registered in the array $err. If that condition is met, the array is searched with in_array() for the name of each of the mandatory variables (the example uses the variable 'sub' -- suburb). Those interested in increasing their PHP skills should attempt to integrate this error checking into the data handling code above.

Join the newsletter!


Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Brand Post

Most Popular Reviews

Latest Articles


PCW Evaluation Team

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?