faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 37 people (76%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How do i pass a value back to a servlet when an ONCHANGE select event occurs?

Sep 6th, 2002 12:22
Stefan Foerster, James Chang,


You can do this by forcing a form submission.

<form id="form1" method="POST" action="/servletURL">
    <select id="select1" onchange="form1.submit();">
        <option>Option 1</option>
        <option>Option 2</option>
    </select>
</form>

The "form1.submit()" tells the form to submit itself. (That was pretty 
obvious I think...) The request object in your servlet will then have a 
parameter "select1" you can check and do what you want with.

I also had a situation where I had a couple buttons I wanted to do 
different things on the server side. But, both submit the form. How do 
you tell them apart? A couple different ways. 1) Give both buttons the 
same id="submitBtn" and different text (value="abc" or value="def"). 
The servlet can check the value of the submitBtn request parameter to 
see which button was pressed. 2) have a hidden field (<input 
id="hiddenField" type="hidden" value=""> and add an onclick event to 
the button that will set the value of hiddenField to the some value 
before calling form1.submit().