BCAV_WEB Posted November 5, 2010 Share Posted November 5, 2010 Hi, I've been developing my companies online site from scratch using XHTML, PHP, MySQL, Ajax etc... I decided to do this rather than a CMS system such as Joomla because I felt that they wouldn't be able to do the functions required by my boss. Anyways long story short, I would like an address finder, street, house no etc... from entering a post code UK mainly, but eventually for the world, if possible as my boss has big ambitions Does anyone have any ideas where I can find a script, coding, or a better yet a tutorial for this functionality. ps. We don't want to pay for any pre-built systems, not bothered ab out putting an advert on our site in return for the system etc... Cheers people Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted November 8, 2010 Share Posted November 8, 2010 Nobody will give you a FREE street search from postcode data as in the UK this data is owned by the Royal Mail. The government did pass legislation that the data should be made open source but as of now I still don't think it is. You will have to check on the Royal Mail website. Postcode data is constantly being updated so it is not cheap to maintain. If you want proper website integration then I suggest http://www.postcodeanywhere.co.uk The other option is to use Google's AJAX search APIs and link in with the maps API. However entering a postcode within Google maps does not always return a street name. Quote Link to comment Share on other sites More sharing options...
BCAV_WEB Posted November 8, 2010 Author Share Posted November 8, 2010 I thought as much, oh well going to have to deal with manual input. Any ideas on post code validation via php Quote Link to comment Share on other sites More sharing options...
Adam Posted November 8, 2010 Share Posted November 8, 2010 Read here for possible UK postcode formats. As for the PHP, have a bash and see what you can do.. Quote Link to comment Share on other sites More sharing options...
Adam Posted November 8, 2010 Share Posted November 8, 2010 I had a quick look on Google (guessed you may look) and the first result isn't the best way of doing it. This would be better (and quicker, in most cases): function validatePostcode($str) { $str = strtolower($str); $str = str_replace(' ', '', $str); switch (strlen($str)) { case 5: // A9 9AA return preg_match('/^[a-z][0-9][0-9][a-z]{2}$/', $str); case 6: // A99 9AA // AA9 9AA // A9A 9AA return preg_match('/^[a-z]([0-9]{2}|[a-z][0-9]|[a-z][0-9][a-z])[0-9][a-z]{2}$/', $str); case 7: // AA99 9AA // AA9A 9AA return preg_match('/^[a-z]{2}([0-9]{2}|[a-z][0-9])[0-9][a-z]{2}$/', $str); } return false; } Quote Link to comment Share on other sites More sharing options...
BCAV_WEB Posted November 9, 2010 Author Share Posted November 9, 2010 i'm guessing $str would be the posted valued from the input form, correct? And in the return false, bit I could have something like $_SESSION['add_customer_errors']["post_code"] = "Please enter an appropriate Post Code"; right? Quote Link to comment Share on other sites More sharing options...
Adam Posted November 9, 2010 Share Posted November 9, 2010 I'd leave the function to just validate the format of the string and return true/false, then check the return value within your main 'application' code: if (!validatePostcode($postcode)) { $_SESSION['add_customer_errors']["post_code"] = "Please enter an appropriate Post Code"; } That way you keep any of the application logic out of your functions, leaving them reusable and your code easier to follow.. Quote Link to comment Share on other sites More sharing options...
BCAV_WEB Posted November 9, 2010 Author Share Posted November 9, 2010 Actually it would have to be, tinking about it. Just tested it and it seems to be working fine with no issues. Just a bit gutted that I can't get a address finder :'( no chance my boss will give me the funds to buy one, he only understands the end product and expects systems done in mear minutes lol [ if (!validatePostcode($_SESSION['add_customer_form'] ["post_code"])) { $_SESSION['add_customer_errors']["post_code"] = "Please enter an appropriate Post Code"; } ] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.