Closed Thread Icon

Topic awaiting preservation: form.elements (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=8511" title="Pages that link to Topic awaiting preservation: form.elements (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: form.elements <span class="small">(Page 1 of 1)</span>\

 
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-04-2003 01:55

I have form fields named like so:

<input type="file" name="upfile[0]">
<input type="file" name="upfile[1]">
<input type="file" name="upfile[2]">

I've done this so they come into my PHP script as an array. I want to check these fields for a value in a javascript function when the form gets submitted but I don't seem to be able to. I tried to do it as mr.maX once told me by accessing the element property of the form like so:

myFunction(form) {

if (form.elements["upfile[0]"].value != '') {
do stuff
}
}

But even though I know that element has a value, it never falls into that if statment to do stuff.

Am I doing this wrong or just missing something?

Thanks


-Butcher-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-04-2003 02:06

Need to see more code to be certain. (No idea how the function is being called, for instance.)

First two things that look suspicious: I've never heard of form.elements, and putting [ and ] in a form element name seems dangerous, although usually weird stuff like that works with the object["property"] syntax.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-04-2003 02:19

Slime

It's being called like this:

<input type=submit onclick="return myFunction(this.form);">

and here's the full function:

myFunction(form) {

if (form.elements["upfile[0]"].value != '') {

openWin();
return true;
}
}

I've also got an openWin funcion that works fine so I didn't bother to list it here.

Also here's the thread that mr.maX said this way was okay, but maybe I'm misunderstanding something now.
http://www.ozoneasylum.com/Forum2/HTML/001289.html

Thanks

-Butcher-

[This message has been edited by butcher (edited 02-04-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-04-2003 02:20

Well, try replacing form.elements with just form and see what happens.

If that doesn't work, try some alert box debugging... stuff like

alert(form.elements["upfile[0]"].value);

to see what's going on.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-04-2003 03:44

Slime

In running the alerts for debugging like you said I fould that I had some problem with the logic of some other if statements in the function (although I'm still not quite sure what). No matter what I tried I couldn't get what I wanted out of one function, but I was able to split it into 2 seperate functions and get it to work like I wanted.

Thanks for all your help, you've been a windfall of knowledge for these forums for a long time now and I'd like to thank you for your great attitude and your willingness to help.



-Butcher-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-04-2003 04:43

No problem. That'll be $50 and a large cheese pizza, please.

By the way... for those reading along, this is one of the main reasons that I usually ask to see the flawed page in action, or to see all of the code. It often happens that the problem in the code isn't right where you think it is; it's usually caused by something else - some logic problem or a typo earlier on, usually where you wouldn't expect it. This is also why debugging with alert() boxes and window.status messages can be really handy (and might save you the trouble of having to wait for a reply on this forum!). I still do it all the time; it's a skill worth learning if you deal with code a lot.

« BackwardsOnwards »

Show Forum Drop Down Menu