i live in the states so needed to add a states field... i've added all the fields and tables so that job posters may select state and then city and post, everything works except i need the ajax code to be added so that the cities get listed based on the state selected, i don't know much javascripting, need this as soon as possible... here's the page that i'm talking about: http://jobspath.com/post/
basically i need the javascript/php/smarty code so that the code for city_id select menu executes after the state_id is passed to it... as a tip for development, since many countries have different regions, it'd be nice to add a regions field like i added the states right before the cities field and have the cities load based on region.. so when someone in the states wants to use it, they can just rename "region" to "state", and ajax loading would already be implemented, no hassle.. the ajax part should be an easy add to what i already have, too bad i don't know java that well, great thanks to anyone who can help
I did this for a UK installation.... javascript: location.js
//Applies cascading behavior for the specified dropdowns function applyCascadingDropdown(sourceId, targetId) { var source = document.getElementById(sourceId); var target = document.getElementById(targetId); if (source && target) { source.onchange = function() { displayOptionItemsByClass(target, source.value); } displayOptionItemsByClass(target, source.value); } }
//Displays a subset of a dropdown's options function displayOptionItemsByClass(selectElement, className) { if (!selectElement.backup) { selectElement.backup = selectElement.cloneNode(true); } var options = selectElement.getElementsByTagName("option"); for(var i=0, length=options.length; i<length; i++) { selectElement.removeChild(options[0]); } var options = selectElement.backup.getElementsByTagName("option"); for(var i=0, length=options.length; i<length; i++) { if (options[i].className==className) selectElement.appendChild(options[i].cloneNode(true)); } }
//Binds dropdowns function applyCascadingDropdowns() { applyCascadingDropdown("state_id", "city_id"); //We could even bind items to another dropdown //applyCascadingDropdown("items", "foo"); }
//execute when the page is ready window.onload=applyCascadingDropdowns;
in functions.php (customer side...not admin...changed one function to:) I basicaly added a state field. For each city in my database, I also added the state it belongs to in a new column
function get_cities() { global $db; $cities = array(); $sql = 'SELECT id, name, state FROM cities ORDER BY id ASC'; $result = $db->query($sql); while ($row = $result->fetch_assoc()) { if (count($cities) < 1) { $cities[] = array('id' => $row['id'], 'name' => '-- ' . $row['name'] . ' --'); } else { $cities[] = array('id' => $row['id'], 'name' => $row['name'], 'state'=> $row['state']); } } return $cities; }
How it works: it's a bit of a hack, but basically, 1. I added an extra column in cities to include their state 2. uploaded a javascript file 3. modified header to include the javascript file (watch out for the name of your state drop down...it's referenced in the javascript file) 4. added an extra drop down, modified the current city drop down 5. talked to my ex-girlfriend online about her cousin messing up her hair (optional)