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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest News Articles


PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?