OK, this used to work but I’ve transfered my code to another server and now it doesn’t work, I’ve been told the only difference is that one of the PHP GLOBAL options has been truned off due to security risk. No biggie, but for some reason my code won’t work.
Purpose – I’m trying to dynamically create an array from a select menu.
Here is the error that I get:
“Error: ‘document.forms.myform.elements’ is null or not an object”
I know what is happening (I think) I just don’t know how to fix it. I think the value that I’m passing is getting lost some how. I tried to add some output statements to see what was happening and this is what I get for output:
IM HERE
this is el:[object]:
el length:0:
SHOW ARRAY
Its missing the addtoarray() function which is whats causing me to think that the value being passed (el) is getting lost somehow…..
Here is the javascript code:
[code]
<SCRIPT TYPE=”TEXT/JAVASCRIPT”>
var selected_array = new Array();
function build_array(el)
{
document.write(“IM HERE<BR>”);
document.write(“this is el:” + el + “:<BR>”);
document.write(“el length:” + el.length + “:<BR>”);
for (var i = 0; i < el.length; i++)
{
var val = el[i].value;
if (el[i].selected)
{
addtoarray(val);
}
else
{
removefromarray(val);
}
}
showArray(val);
}
function addtoarray(val)
{
document.write(“ADD TO ARRAY<BR>”);
for (var i = 0; i < selected_array.length; i++)
{
if (selected_array[i] == val) return;
}
selected_array[i] = val;
}
function removefromarray(val)
{
document.write(“REMOVE FROM ARRAY<BR>”);
for (var i = 0; i < selected_array.length; i++)
{
if (selected_array[i] == val)
{
selected_array.splice(i, 1);
return;
}
}
}
function showArray(array)
{
document.write(“SHOW ARRAY<BR>”);
arraytext = “”;
for (var i=0; i < selected_array.length; i++)
{
if(i == 0)
arraytext += selected_array[i];
else
arraytext += “:” + selected_array[i];
}
document.forms[‘myform’].elements[‘array_build’].value = arraytext;
}
</SCRIPT>
Here is the form that is making the function call (mixture of PHP and HTML):
[code=php]
<FORM NAME=”myform”>
<SELECT NAME=”myselect” MULTIPLE=”multiple” SIZE=”10″ ONCHANGE=”build_array(this)”>
<?php
while ($row = $result->fetchRow($mode))
{
$trimed = trim($row[0]);
echo “<OPTION VALUE=”$trimed”>$trimed</OPTION>”;
}
$result->free();
?>
</SELECT>
<BR>
<INPUT TYPE=”hidden” NAME=”array_build” ><BR>
<INPUT TYPE=”submit” VALUE=”SELECT”>
<INPUT TYPE=”reset” VALUE=”RESET”><BR>
</FORM>
anybody got any ideas????