SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    165
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Persist Selected Dropdown Options

    Hi,

    I'm building an ajax/php/mysql application.
    I have 3 dependent dropdowns. I use ajax to avoid the page reload on every selectbox change.

    But if I reload the page after I drill down the 3 dropdowns, only the first dropdown maintains it's selected option, because I did it with php.

    But I'm trying to do it for the 2nd and 3rd dropdown with javascript:

    <code>
    $marca = $_GET['IDMarca'];
    echo "obj.options[obj.options.length] = new Option('-Seleccione-','0');\n";
    while($resmodelo = mssql_fetch_array($queryModelos))
    {
    echo "obj.options[obj.options.length] = new Option('".$resmodelo['Descricao']."','".$resmodelo['IDModelo']."');\n";
    echo "for (var i=0; i < obj.options.length; i++) {\n";
    echo "alert('obj.options[i].selectedIndex.Value');";
    echo " if (obj.options[i].selectedIndex.Value = ".$marca.") {\n";
    echo " obj.options[i].selected = true;}\n";
    echo "}\n";
    }
    </code>

    But... it's not working. I get the error:
    Error: obj.options[i].selectedIndex has no properties

    Please help.

    Mário
    Yours truely
    Mário Ramos

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you dont show where obj is defined.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    165
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    My 1st and 2nd dropdowns have an onchange event defined. For example the 1st one (called ddlMarca) calls the function getModelos, which in turn calls createModelos.

    Here's the code. Hope it helps to clear my question.


    <code>
    var ajax = new sack();

    function getModelos(sel)
    {
    var IDMarca = sel.options[sel.selectedIndex].value;
    //Empty model select box
    document.getElementById('ddlModelo').options.length = 0;
    if(IDMarca.length>0){
    //Specifying which file to get
    ajax.requestFile = 'getMV.php?IDMarca='+IDMarca;
    //Specify function that will be executed after file has been found
    ajax.onCompletion = createModelos;
    ajax.runAJAX(); // Execute AJAX function
    }
    }

    function createModelos()
    {
    var obj = document.getElementById('ddlModelo');
    //Executing the response from Ajax as Javascript code
    eval(ajax.response);
    }

    </code>
    Yours truely
    Mário Ramos


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
  •