#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep Power
    0

    JavaScript and Servlets


    I would like to have a select list that, upon clicking of an option, calls a servlet with method Post. I am trying to avoid the use of a submit button. Is this possible?

    I have used the onChange attribute on the select list, and it successfully calls the servlet. However, it is using the Get method so I am losing my request parameters.

    Can anyone help?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    200
    Rep Power
    14
    Do document.forms[0].submit in your onChange (I suppose your form declaration takes care of the method and target)

    or add a querystring when you go for that GET method
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep Power
    0
    That document.forms[0].submit didn't do anything even though the form does have the servlet name and method post. It seems like a logical solution.

    I'm not a strong JavaScript person (mostly JSP and Java servlet server-side code). Does that have to be in quotes, out of quotes or any other weird syntactical things I should know about? And what is the meaning of the 0 index?

    I tried quotes, no quotes, index 0 and index 1...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    200
    Rep Power
    14
    Ok, your document should resemble:

    <HTML>
    <HEAD>
    <TITLE></TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function foo()
    {
    document.forms[0].submit();
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM ACTION="servlet_whatever" METHOD="POST">
    <SELECT NAME="myListbox" onChange="foo();">
    <OPTION VALUE="1">Option 1</OPTION>
    <OPTION VALUE="2">Option 2</OPTION>
    </SELECT>
    </FORM>
    </BODY>
    </HTML>

    it's document.forms[0] because a document can contain several <FORM></FORM> declarations... so document.forms is a collection and [] is used for indexing
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep Power
    0
    Got it!!

    It was the missing parens that had me.

    Thank you for your help...

IMN logo majestic logo threadwatch logo seochat tools logo