Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how can i access a dynamically named variable?

    Well, here's what it does. some php echos out a bunch of javascript variables. then the user makes a select box choice. the select box choice value equals the 'dt number', which is part of the javascript variables outputted by php. so i then need to be able to have javascript access a dynamically named variable.

    Thus:

    Code:
    function selector()
    {
    var selected = document.selectForm.selectBox.options[document.selectForm.selectBox.selectedIndex].value;
    
    <?
    // import employee data into javascript //
    $sql2 = "SELECT dt FROM employment";
    $sql2 = mysql_query($sql2);
    while ($row = mysql_fetch_array($sql2, MYSQL_ASSOC))
     {      
     foreach ($row as $dt) 
      { 
      $sql3 = "SELECT name FROM employment WHERE dt=".$dt;
      $sql3 = mysql_query($sql3);
      $name = mysql_result($sql3,0);
      echo "var dt" . $dt . "_name = '" . $name . "';\n";
      }
     }
    ?>
    
    var current = 'dt' + selected + '_name';
    document.write(current);
    }

  • #2
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right now it just prints:

    dtXXXXX_name

    That's the NAME of the variable. I want it to OUTPUT the CONTENT of that variable.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    echo js array instead of normal variables.
    Code:
    function selector()
    {
        var dt = new Array();
        ...
        <?
             ...
            echo "dt[" . $dt . "] = '" . $name . "';\n";
        ?>
        document.write(dt[selected]);
    }

  • #4
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm getting 'dt is undefined', on the first line of dt[xxxxx] = 'somebody's name'...

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Did you add var dt = new Array(); in the first line of the function?

  • #6
    New Coder
    Join Date
    May 2004
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You Are The Bizomb!


  •  

    Posting Permissions

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