Add an address lookup to your web forms – Part 1

In this tutorial I’m going to show you how to add an inline address lookup to your web forms using the PostcodeAnywhere webservice and the Yahoo! Connection Manager.

Get a PCA account

First, you’ll need to sign up to Postcode Anywhere and add some credits. A beginner pack of 300 credits will set you back £25+VAT and will get you 300 address lookups (if you only want the basic details returned for each address).

Use PHP to build the webservice client

An AJAX lookup works by passing some data to a script on the webserver, which performs some translation (in our case, connects to the webservice) and returns the result to the page where javascript makes the updates. Create a new php file on your webserver call getpostcode.php

Set up the defaults for the lookup:

<?php

$account_code = 'PCA_ACCOUNT';
$license_code = 'PCA_LICENCE';

$postcode = '';
$building = '';

?>

Then, get the postcode and building number or name from the query string:

if ( array_key_exists('postcode', $_QUERY) ) {
	$postcode = $_QUERY['postcode'];
}

if ( array_key_exists('building', $_QUERY) ) {
	$building = $_QUERY['building'];
}

Then we have to build the REST url that will ask the webservice for the address:

$url = 'http://services.postcodeanywhere.co.uk/csv.aspx?';
$url .= 'account_code=' . $account_code;
$url .= '&license_code=' . $license_code;
$url .= '&action=fetch';
$url .= '&postcode=' . $postcode;
$url .= '&building=' . $building;
$url .= '&style=simple';

Use the php file function to get the data from the webservice into a variable called $data

$data = file($url);

We’ve asked the webservice to return the data in csv format, so we’re expecting two lines. The first will contain the field names and the second will contain the values. First, we want to return an error if the webservice call failed:

if(!$data) {
	die('{"status":"error"}');
}

The status: error message is in JSON format so that it can be understood by the javascript which will handle the address lookup.

The php file function returns an array, with each line in a different array element – so the first line will be in $data[0] and the second in $data[1] (arrays always start at 0 not 1!).

Next we want to trim any whitespace such as spaces or newline characters from the end of each line:

$data[0] = trim($data[0]);
$data[1] = trim($data[1]);

then we trim all extra speech marks from the start and end to help us split the lines up

$data[0] = trim($data[0], '"');
$data[1] = trim($data[1], '"');

Now we can split the lines into two arrays, one for field names and one for values

$field_names = explode('","', $data[0]);
$field_values = explode('","', $data[1]);

and throw up an error if the number of fields isn’t the same as the number of values

if(count($field_names) != count($field_values)) {
	die('{"status":"error"}');
}

Empty the $data array and loop through the field names and values, building up an array of name => value pairs

$data = array();

for($counter = 0; $counter < count($field_names); $counter++) {
	$data[$field_names[$counter]] = $field_values[$counter];
}

PostcodeAnywhere returns a field called error_number if there’s a problem in the request. If this field exists, return an error to the form:

if(array_key_exists('error_number', $data)) {
	die('{"status":"error"}');
}

Finally, output the data in JSON format:

$output = '';
foreach($data AS $key=>$value) {
	$output .= '"' . $key . '":"' . $value . '",';
}
//$output = trim($output, ',');
$output = '{"status":"ok",' . $output . '}';
echo $output;

In part 2, we’ll look at how to use the Yahoo Connection Manager to put this data on your form. Check back in a few days…

This entry was posted in code. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

3 Responses to Add an address lookup to your web forms – Part 1

  1. Iva says:

    Hi

    I don’t know where else to contact you so I apologise for an off-topic comment, but I found a little sort-of-a-bug in Pages+ …email me for details, as I don’t want to go off-topic here.

  2. Adam says:

    Good example.

    Just a quick note to suggest porting your example to CraftyClicks.co.uk – it is based on the same databse (the Royal Mail PAF) but much better priced (http://www.craftyclicks.co.uk/prices).

    Cheers,
    Adam.

  3. MrWebService says:

    Ooh hello – nice guide.
    I’m actually going to admit I work for PCA! ;) .
    Stick with us – we have two servers and 99.999% availability ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>