Retrieving form values in Javascript

Discussion in 'Javascript' started by namanhvu@gmail.com, Sep 29, 2006.

  1. Guest

    Hi everyone,

    I am trying to create a form for authorised users to upload photos to
    an image gallery. I'm attempting to perform simple validation of the
    fields by ensuring the fields aren't empty and that the extensions are
    right. However, I can't seem to access the actual values of the fields
    to start with.

    If I simplify my problem, can anyone tell me why this would work in a
    Javascript alert:

    alert(document.form.Photo1.value);
    //This outputs 'D:/images/x.jpg'

    but this won't:

    var photonumber = "Photo" + 1;
    alert(document.form.photonumber.value);
    // I get "undefined"

    The reason I'm doing this is because I'm looping through a finite set
    of images and need to check each one is valid.

    Is it because I'm not initialising the "photonumber" string correctly?

    If there are any geniuses out there, please help this newbie.

    Thanks in advance!

    Nat.
     
    , Sep 29, 2006
    #1
    1. Advertising

  2. web.dev Guest

    wrote:
    > Hi everyone,
    >
    > I am trying to create a form for authorised users to upload photos to
    > an image gallery. I'm attempting to perform simple validation of the
    > fields by ensuring the fields aren't empty and that the extensions are
    > right. However, I can't seem to access the actual values of the fields
    > to start with.
    >
    > If I simplify my problem, can anyone tell me why this would work in a
    > Javascript alert:
    >
    > alert(document.form.Photo1.value);
    > //This outputs 'D:/images/x.jpg'
    >
    > but this won't:
    >
    > var photonumber = "Photo" + 1;
    > alert(document.form.photonumber.value);
    > // I get "undefined"


    >From your description it sounds like the most likely place it is

    causing an error is in Internet Explorer. That is because you are
    using a variable name that is the same as the form name. Try a
    different variable name.
     
    web.dev, Sep 29, 2006
    #2
    1. Advertising

  3. Lee Guest

    said:
    >
    >Hi everyone,
    >
    >I am trying to create a form for authorised users to upload photos to
    >an image gallery. I'm attempting to perform simple validation of the
    >fields by ensuring the fields aren't empty and that the extensions are
    >right. However, I can't seem to access the actual values of the fields
    >to start with.
    >
    >If I simplify my problem, can anyone tell me why this would work in a
    >Javascript alert:
    >
    >alert(document.form.Photo1.value);
    >//This outputs 'D:/images/x.jpg'
    >
    >but this won't:
    >
    >var photonumber = "Photo" + 1;
    >alert(document.form.photonumber.value);
    >// I get "undefined"
    >
    >The reason I'm doing this is because I'm looping through a finite set
    >of images and need to check each one is valid.
    >
    >Is it because I'm not initialising the "photonumber" string correctly?


    No, it's becauase the elements of a reference designation in
    that notation are literal values, not variables. If you want
    to use variables, you use a different notation:

    alert(document.form.elements[photonumber].value);


    --
     
    Lee, Sep 29, 2006
    #3
  4. Randy Webb Guest

    web.dev said the following on 9/28/2006 9:11 PM:
    > wrote:


    <snip>

    >> but this won't:
    >>
    >> var photonumber = "Photo" + 1;
    >> alert(document.form.photonumber.value);
    >> // I get "undefined"

    >
    >>From your description it sounds like the most likely place it is

    > causing an error is in Internet Explorer. That is because you are
    > using a variable name that is the same as the form name. Try a
    > different variable name.
    >


    No, it is because there is no form element named "photonumber", and it
    won't replace the variable with it's value in dot notation. What will
    work however is something like this:

    document.forms['form'].elements[photonumber].value

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Sep 29, 2006
    #4
  5. JRS: In article <>,
    dated Thu, 28 Sep 2006 17:18:30 remote, seen in
    news:comp.lang.javascript, posted :

    >var photonumber = "Photo" + 1;
    >alert(document.form.photonumber.value);
    >// I get "undefined"


    >If there are any geniuses out there, please help this newbie.


    One only needs the ability to read the newsgroup FAQ, 2.3, 4.41 & 4.39;
    see below.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 30, 2006
    #5
  6. Guest

    Thanks to everyone who replied.

    I got it to work by doing the following:

    alert(document.form.elements[name].value);

    Nat.


    Dr John Stockton wrote:
    > JRS: In article <>,
    > dated Thu, 28 Sep 2006 17:18:30 remote, seen in
    > news:comp.lang.javascript, posted :
    >
    > >var photonumber = "Photo" + 1;
    > >alert(document.form.photonumber.value);
    > >// I get "undefined"

    >
    > >If there are any geniuses out there, please help this newbie.

    >
    > One only needs the ability to read the newsgroup FAQ, 2.3, 4.41 & 4.39;
    > see below.
    >
    > --
    > © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    > <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    > <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    , Oct 1, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Davidson
    Replies:
    0
    Views:
    431
    Mark Davidson
    Aug 11, 2003
  2. ani
    Replies:
    0
    Views:
    370
  3. Bryan Donaldson
    Replies:
    0
    Views:
    439
    Bryan Donaldson
    Nov 24, 2003
  4. Steve C. Orr [MVP, MCSD]

    Re: Retrieving Msgbox Values From Client

    Steve C. Orr [MVP, MCSD], May 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    399
    Steve C. Orr [MVP, MCSD]
    May 12, 2004
  5. Kant
    Replies:
    3
    Views:
    91
    ZeBest Kant
    Jan 19, 2004
Loading...

Share This Page