If you are developing a web application or website that requires user to provide a location (address of their house, business, establishment etc) for visitors in Philippines, It might be useful for them to select from a list of Cities or Barangay instead of typing everything in a text field.
There is around approximately 42,000 plus barangays in the Philippines. Let alone the list of cities.
Storing this amount of data just to display this kind of dropdown is too much of hassle and load for most applications.
Not to mention, that you need to have the copy of this data in every database that needs this, there should be a better way of doing it...
I've developed a jQuery Plugin that allows you to turn any <select></select>
element into a dynamic one, so that its list of
<options></options>
is automatically populated by data via AJAX request.
The AJAX request is being initiated into a FREE REST API (that I've also developed)
which is pretty much responsible for returning the list of items depending on the filter and option you specified when invoking the plugin.
Here is a very simple demo of what the plugin can do. Below, you could see we have 4 dropdowns.
Each is being populated dynamically by the plugin, depending on the value of the dropdown above it
See LIVE DEMO
// attach handlers when user had selected an item in the dropdown
$('#region').on('change', function(){ // fill province dropdown });
$('#province').on('change', function(){ // fill cities dropdown });
$('#city').on('change', function(){ // fill barangay dropdown });
// call the plugin to those dropdowns with the proper location_type
$('#region').ph_locations({'location_type': 'regions'});
$('#province').ph_locations({'location_type': 'provinces'});
$('#city').ph_locations({'location_type': 'cities'});
$('#barangay').ph_locations({'location_type': 'barangays'});
// fill up the regions dropdown, so user can start "drilling down"
$('#region').ph_locations('fetch_list');
Note that this plugin doesn't requires you to install any backend script in your app. You can use it in any static website that have jQuery. Interested? head over to the Installation to start using it!
<script src="https://f001.backblazeb2.com/file/buonzz-assets/jquery.ph-locations-v1.0.2.js"></script>
jquery.ph-locations-v1.0.2.js
somewhere in your server and reference it.
create the markup
<select name="city" id="my-city-dropdown"></select>
$('#my-city-dropdown').ph_locations({'location_type': 'cities'});
$('#my-city-dropdown').ph_locations( 'fetch_list', [{"province_code": "1339"}]);
You can also check the source code of the demo to see how it is used.
You can show your support and appreciation by Buying me a coffee (I love coffee!).
The project is open-source and can be found in this GitHub repo. If you happen to find any bug or improvements that could be made to better cater to your project, feel free to open an issue
By default, the plugin includes a "free api key" for general use. It is usable but have limits of 5000 calls per day across all free users of the free plugin.
Once the 5000 calls per day is exhausted, the api will start returning error ( and plugin will not work).
The Free API key is generally okay to use for demos and one-time projects.
But for commerial use cases and you need to ensure the plugin is working in your app 100% of the time, you need to have your OWN API key.
What comes with the Paid API key?