Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthormir100
    • CommentTimeFeb 19th 2008
     
    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/

    in the publish-write.tpl i have

    <select name="state_id" id="state_id" tabindex="3" onchange="citybystate(this.value)">
    {section name="s" loop=$states}
    <option value="{$states[s].id}" >{$states[s].name}</option>
    {/section}
    </select>

    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
    • CommentAuthorbarry
    • CommentTimeApr 26th 2008
     
    me too!
    • CommentAuthorhitwill
    • CommentTimeMay 20th 2008
     
    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;
    • CommentAuthorhitwill
    • CommentTimeMay 20th 2008
     
    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;
    }
    • CommentAuthorhitwill
    • CommentTimeMay 20th 2008
     
    header.tpl...

    added:
    <script src="{$BASE_URL}js/location.js" type="text/javascript"></script>

    in the place where scripts are included...like line 25
    • CommentAuthorhitwill
    • CommentTimeMay 20th 2008
     
    in publish-write.tpl...

    write above the dropdown for cities...added the code for a state dropdown...

    <select name="state_id" id="state_id" tabindex="3" {if $job.location_outside_ro != '' OR $smarty.post.location_outside_ro_where != ''}disabled="disabled"{/if}>

    <option value="London">London</option>
    <option value="Essex">Essex</option>
    <option value="Hertfordshire">Hertfordshire</option>
    ...truncated.....
    <option value="Worcestershire">Worcestershire</option>
    <option value="Yorkshire">Yorkshire</option>


    </select>
    <br />
    • CommentAuthorhitwill
    • CommentTimeMay 20th 2008
     
    and in the same file...modified the cities dropdown to give them a class:

    <select name="city_id" id="city_id" tabindex="4" {if $job.location_outside_ro != '' OR $smarty.post.location_outside_ro_where != ''}disabled="disabled"{/if}>
    {section name="c" loop=$cities}
    <option class="{$cities[c].state}" value="{$cities[c].id}" {if $smarty.post.city_id == $cities[c].id || $job.city_id == $cities[c].id}selected="selected"{else}{if $user_city.id == $cities[c].id AND !$smarty.post.city_id AND !$job.city_id}selected="selected"{/if}{/if}>{$cities[c].name}</option>
    {/section}
    </select>
    • CommentAuthorhitwill
    • CommentTimeMay 20th 2008
     
    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)