SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question javascript onload select value form

    Hello

    I have a linked select menu. When the page loads, I have it poplulate a value from a database.

    This is a state / city select menu, which I would like the cities to load based on the state selected.

    This works fine if a new record is created but if I have to edit a record I would like to have the state selected and the city selected. Please let me know if you have any ideas or need further explanation. below is the code

    code that works fine for new record
    Code JavaScript:
     
    function getCityList(sel)
    {
    var usState = sel.options[sel.selectedIndex].value;
    document.getElementById('usCity').options.length = 0; // Empty city select box
    if(usState.length>0){
    var index = ajax.length;
    ajax[index] = new sack();
     
    ajax[index].requestFile = 'getCities.php?usState='+usState; // Specifying which file to get
    ajax[index].onCompletion = function(){ createCities(index) }; // Specify function that will be executed after file has been found
    ajax[index].runAJAX(); // Execute AJAX function
    }
    }
    function createCities(index)
    {
    var obj = document.getElementById('usCity');
    eval(ajax[index].response); // Executing the response from Ajax as Javascript code 
    }


    my attempt to have this value display on load

    Code JavaScript:
     
    <script language="javascript">
     
    function getCityList3(sel)
    {
    var usState = document.getElementById('usState').value;
    document.getElementById('usCity').options.length = 0; // Empty city select box
    if(usState.length>0){
    var index = ajax.length;
    ajax[index] = new sack(); 
    ajax[index].requestFile = 'getCities.php?usState='+usState; // Specifying which file to get
    ajax[index].onCompletion = function(){ createCities(index) }; // Specify function that will be executed after file has been found
    ajax[index].runAJAX(); // Execute AJAX function
    }
    }
     
     
    window.onload = getCityList3(this);
    </script>

  2. #2
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any ideas?

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the form has already been filled out and you are "editing" it you should have the already selected city and state stored somewhere. I suspect you will either have to change your createCities function to allow setting the selected one, or modify the javascript that is generated by your ajax request (which is "eval"d). Can you show us what is being returned by the ajax request?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •