Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to auto retrieve the address,state, zipcode from an address and save in database

I have done the auto searching address that map to google maps. so when i search for an address it will autocomplete and suggested a few address that match the keywords i typed. so it is a standard format address form google.

Let said I having an address and i want to retrieve an address string with address, state, zipcode and save in my database with differents fields.

I have been searching from google but can't find the answer. Is there a possible way to do that?

Kenny, could you explain/expand on your question a little more. I'm confused as to what it is you are trying to do.

Hi Andrew,

I have a text box and when i type my keyword on the text box it auto search for me the nearest address that i'm key in. it is because i already use the google map api.

let said i have an address of University of Liverpool, Liverpool, Merseyside L69 3BX, UK

street name: University of Liverpool, Liverpool, Merseyside zipcode: L69 3BX state: UK

in my database i have street_name, zipcode and state field. How can i auto retrieve the street name, zipcode and state and store in my database?

1 Answer

Jacob Herper
Jacob Herper
94,150 Points

I was facing the same problem before and couldn't find a solution. You could use the PHP explode() function to split the address entered into pieces after each comma. The main issue is, that Google's auto complete doesn't add a post code and state by default. In fact if I type in "University of Liverpool" it will only auto complete to "University of Liverpool, Liverpool" (thats from within the UK, if you use google maps outside the UK it might add the country at the end, but no post code).

Another problem I was facing was, that the address format is different for different countries, for example in Germany the format is Street, Post Code, City, Country (another example: What happens if there are two address lines instead of one, that are comma separated?). Therefore it is almost impossible to split it up and import it into the database correctly.

Please correct me if I'm wrong, but I found it to be easier to just have separate fields for all parts of the address, as everything I tried to resolve the problem caused wrong database entries (e.g. country name was inside the post code column and country column was empty).

Don't get me wrong: it's a great feature if you want to look for an address and show it on a google map, but otherwise I would suggest using a regular form.

Thanks Jacob,

I guess i have to put zipcode and state textbox for user to key in to able store correctly inside the zipcode and state field in database. And the address will store like this: University of Liverpool, Liverpool, Merseyside L69 3BX, UK

Jacob did you know Google map can only search for city, state and country?