/    Sign up×
Community /Pin to ProfileBookmark

need some help

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>
[/code]

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>
[/code]

anybody got any ideas????

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63May 12.2003 — It looks like you are referencing the form incorrectly.

change thisline

document.forms.myform.elements

to this

document.myform.elements
Copy linkTweet thisAlerts:
@mearnhauthorMay 12.2003 — HUH???? ?

Would you mind expounding on that a little????

Are you refering to this line:

document.forms['myform'].elements['array_build'].value = arraytext;

What exactly would it look like?

TIA,

mike
Copy linkTweet thisAlerts:
@mearnhauthorMay 12.2003 — :rolleyes:

Well, as I suspected. its not passing the parameters correctly to the function build_array().

I added this code:
<i>
</i>function show_props(obj, obj_name)
{
document.write("INSIDE SHOW PROPERTIES&lt;BR&gt;");
var result = "";
for (var i in obj)
{
result = obj_name + "." + i + " = " + obj[i] + "&lt;BR&gt;";
document.write(result);
}
return result
}


and I get back a bunch of stuff similar to this, but the interesting part is the .value object is null which means it isn't even passing the parameter correctly:
<i>
</i>INSIDE SHOW PROPERTIES
el.language =
el.scrollHeight = 0
el.isTextEdit = true
el.currentStyle = null
el.document = [object]
el.onmouseup = null
...
...
...
el.size = 20
[B]el.value = [/B]
el.border =


anybody else have any ideas?

I'm still looking for a resolution.

TIA,

mike
Copy linkTweet thisAlerts:
@khalidali63May 13.2003 — I guess you meant this

document.forms['myform'].elements['array_build'].value = arraytext;

was not incorrect,

my bad...I overlooked that statement...

But what I wrote from the error

document.forms.myform.elements

that was incorrect.

:-)
Copy linkTweet thisAlerts:
@khalidali63May 13.2003 — Ohhkay...lets say it this way..May be I used a wrong phrase...

in the

document.forms.myform.elements,

"forms" is redundant....
Copy linkTweet thisAlerts:
@mearnhauthorMay 13.2003 — are these two statments the same?

document.forms['myform'].elements['array_build'].value = arraytext;

document.myform.array_build.value = arraytext

mike
×

Success!

Help @mearnh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...