What is the best way to process input that may be from several countries?

Most of my websites in the past were limited only to the United States when it came to displaying addresses. In the project I'm currently working on, users can add events from around the world. My problem is how to solve different ways of displaying addresses around the world. For example, City / State / Zip is just a thing in the USA.

I figured I would change the input displayed based on the selected country, but I have no idea how I should know how each country makes addresses.

Ideas?

+6
php street-address
source share
4 answers

First of all, be careful what you make mandatory. Do not use checks such as "Zip code should only contain numbers."

Then you should have the minimum number of fields - Country, City, Zip code and 2 lines for the address.

See, for example, this book ordering a worldwide delivery site. Put the book in the basket and go to the cashier to see: https://securepayment.bookdepository.co.uk/checkout/summary

+2
source share

Check this site, it has information about the format of the address for all countries, you can use it as a link. International Address Formats

+2
source share

As I can see, you have 2 options:

1 - find how each country displays its addresses and creates a template for each

$address1='23 main st'; $city='New York'; $state='NY'; $postalcode='10000'; $country='US'; include("address_format_" . $formats[$country] . ".php"); 
Formats

would display the country in a format, allowing several countries to have the same format.

2 - display of all addresses in a common way:

 <address> <?php if($address1): ?>Address: <?php echo $address1 ?><br><?php endif; ?> <?php if($city): ?>City: <?php echo $city ?><br><?php endif; ?> <?php if($state): ?>State: <?php echo $state ?><br><?php endif; ?> <?php if($postalcode): ?>Postal Code: <?php echo $postalcode ?><br><?php endif; ?> </address> 
+1
source share

Two solutions that I can think of are as follows:

  • [Complex] Change the address requirements based on country.
  • [Simple] Just specify the drop-down list of the country and only one address field in which the user can enter the address, but he wants you to simply display the contents of this field.

If you really need an address that needs to be divided into several fields, I think that a simple way to store the entire address in one field may be the best. Of course, this means that your address table will not be normalized, and you will not be able to verify the address entered by the user, although the verification will be quite complicated, depending on the design of your code. But this is a balancing act between usability, design and complexity.

Hope this helps.

0
source share

All Articles