Creating a driving directions link for your customers
published 16.May.2007
Background
After I created the "driving directions" widget for k9directory.co.uk, I have had many request from web site owners and developers on how I created it, rather than keep replying to each request in turn, I decided to write this short article explaining how it can be achieved.
If you have a web site or directory that lists physical locations that people can visit, then it's handy to offer your visitors a "driving directions" link to these locations. If you want to see how it works, then visit the Chestnut Cottages page on the k9directory web site, and you will notice the driving directions section on the right hand panel.
You don't want your visitors having to re-enter their postcode at every page on your site, so the system takes the user to a web form where they input their postcode once, the system then set's a cookie on the users machine, which it then uses on subsequent page requests to display a "driving directions" link between the users postcode and the postcode of the location your web site is talking about.
Capturing the users postcode
Below is the code for the set_postcode.php script which allows you to use the same page for setting the postcode, and allowing the user to edit their stored postcode. It first checks to see if the cookie my_postcode has been set, and if it has then outputs a text entry box with the postcode value entered for editing, or presents the user with a blank field.
The script also creates two hidden form fields for page name and return URL, which will become clearer later in this tutorial.
set_postcode.php :-
<form method="post" action="update_postcode.php">
Enter your full postcode e.g. PR6 0LA
<?php
if(isset($_COOKIE['my_postcode'])) {
echo '<input type="text" name="postcode" value="',
urlencode($_COOKIE['my_postcode']), '" />';
} else {
echo '<input type="text" name="postcode" value="" />';
}
if(isset($_REQUEST['name'])) {
echo '<input type="hidden" name="name" value="',
urlencode($_REQUEST['name']), '" />';
}
if(isset($_REQUEST['return'])) {
echo '<input type="hidden" name="return" value="',
urlencode($_REQUEST['return']), '" />';
}
?>
<input type="submit" value="Set My Postcode" />
</form>
The above form submits it's values to a script called update_postcode.php which will set the cookie.
Saving the entered postcode to a cookie
Below is the code for update_postcode.php, which takes the form values and sets a cookie with a long life, so that the chances are it will still be active when the user returns to your site at a later date. After setting the cookie, the script outputs a thank you message, and creates a return link to the calling web page.
update_postcode.php :-
<?php
$postcode = strip_tags(strtoupper(trim($_REQUEST['postcode'])));
$postcode = str_replace(" ", "", $postcode);
setcookie('my_postcode', $postcode, time()+60*60*24*30);
echo '<p><b>Thank you</b><br />';
echo 'Your postcode has been set to <b>';
echo $postcode;
echo '</b></p>';
echo '<p>Any property page you now visit will have
a driving directions link from ';
echo $postcode, ', to the accommodation.</p>';
if(isset($_POST['return'])) {
echo '<p><a href="',
strip_tags(urlencode($_POST['return']));
echo '">return to : ',
htmlentities(urldecode($_POST['name'])), '</a></p>';
} else {
echo '<p><a href="index.html">Continue</a></p>';
}
?>
Outputting the driving directions link on the property pages
To obtain the PHP code required to create the driving directions link, you need to purchase the 4 page PDF report which explains all the PHP code, and demonstrates how to implement it on your web site. The report only costs £4.99
