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.

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

Gavin Sherry

PC World

Comments

Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

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.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?