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:
Suburb:
Post code:
State:
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";
fclose($fp);
} else {
echo "Write to address book was invalid. Data may
be corrupted!";
fclose($fp);
exit();
}
}
?>

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

Essentials

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Michael Hargreaves

Windows 10 for Business / Dell XPS

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

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

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.

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?