...

View Full Version : how can i access a dynamically named variable?



questor
08-11-2004, 02:41 AM
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:



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);
}

questor
08-11-2004, 02:42 AM
Right now it just prints:

dtXXXXX_name

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

glenngv
08-11-2004, 03:03 AM
echo js array instead of normal variables.


function selector()
{
var dt = new Array();
...
<?
...
echo "dt[" . $dt . "] = '" . $name . "';\n";
?>
document.write(dt[selected]);
}

questor
08-11-2004, 03:51 AM
I'm getting 'dt is undefined', on the first line of dt[xxxxx] = 'somebody's name'... :confused:

glenngv
08-11-2004, 03:58 AM
Did you add var dt = new Array(); in the first line of the function?

questor
08-11-2004, 03:59 AM
You Are The Bizomb!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum