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

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

SanDisk MicroSDXC™ for Nintendo® Switch™

Learn more >

Breitling Superocean Heritage Chronographe 44

Learn more >

Toys for Boys

Family Friendly

Panasonic 4K UHD Blu-Ray Player and Full HD Recorder with Netflix - UBT1GL-K

Learn more >

Stocking Stuffer

Razer DeathAdder Expert Ergonomic Gaming Mouse

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

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.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

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?