Link to home
Start Free TrialLog in
Avatar of Atomskish
Atomskish

asked on

Accessing html form values using pure Java?

Currently, I have a jsp that implements a dropdown list in html:

<form action="" method="POST" onSubmit=/*doesStuff*/) name="aForm">
    <SELECT NAME="paymentTypeList" onChange="getValue()">
        <OPTION value="1" selected>VISA</OPTION>
        <OPTION value="2">MasterCard</OPTION>
        <OPTION value="3">American Express</OPTION>
        <OPTION value="4">COD</OPTION>
    </SELECT>


The function getValue() is as follows:


function getValue()
{
  globalPaymentType = document.aForm.paymentTypeList.value;
}  

...where globalPaymentType is a global variable.




This is all well and good, but when it comes time to make an update  to the server (done in Java) I can't access the JavaScript value globalPaymentType, which is a problem. As such, I need a function written in Java that will accomplish roughly the same thing. Other good methods are welcome, also.


As always, any comments/examples are greatly appreciated.
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Atomskish
Atomskish

ASKER

After form submission I tried request.getParameter("globalPaymentType"), but the printout just says "null". Is request.getParameter suppoed to work with javascript variables?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
^^ Submitting the form onChange() gets me the correct value when I request it, but ends up refreshing the page (resetting the dropdown selection back to the default selected, VISA). Is there any way to prevent this?
u can use javascript init function to set the value back.

or else
u can use a submit button and submit the form to prevent the refresh.
jsp just needs the form to be submitted to get the values
can i know more on what u r tryin to do...
bcos its not necessary to submit the form repeatedly
u can use javascript to achieve some dynamic solutions in the front end itself and i can help u with that
Well, the payment dropdown list is not the real form I need to submit - all I need from it is a way to grab the currently selected value whenever the user changes it, which is a pain using java. In javascript I don't have to submit the whole form to get the value.

Submitting my main form calls a validing function (in javascript) which in turn calls other functions. Once I start into those functions, document.aForm.submit() doesn't return the correct value anymore; I need to submit the form containing the dropdown information before I submit my main form. Is there some way to do this without refresing the page?
Use the hidden field on you main form, holding the information you need. And whenever user will change value on the dropdown list, call the JS function, that copies selected value to the hidden field on the main form.

> resetting the dropdown selection back to the default selected, VISA

You will have to get this attribute back from the server. If server is just forwarding the request, the request attributes should be still there when you get back to the JSP page. Just try to read the request variable with the scriptlet, and set the dropdown list to appropriate value. However, if server is redirecting the request, you will have to copy request variable to the newly created request object.

Or, alternatively, you can use a framework like Struts, which allows you to automatize this process.
try havin them in different forms
I tried splitting up the forms, but it's the same result.

The strange thing is that the form already has a "submit" button (input type="submit"), but if I use only that, the requested values will be null when I print them out. Another odd thing is that I have several text fields in the same form for the purposes of inputting an address. If I submit the form prior to clicking on the "submit" button (through onChange) and request the values the same way as I do for the payment table  (request.getParameter), ONLY the fields consisting totally of digits (i.e. the zip code field) will print out a non-null value. I also see that the payment type values (the only other non-null values) are integers only as well (0 to 5). Some relation?  
Uh..disregard that last part about only numbers printing...I forgot to add "" inside the alert(), stupid me.
So... How does it look now, to sum it up? Do you have two forms (the "main" form, and the "dropdown" form)? Do you have text (or hidden) field on the main form? What's the "action" parameter of the main form? What's the "method" parameter of the main form? How does your "onChange" method looks like? How do you display the requested values?
When the dropdown list changes, the selected value is assigned to a hidden text box in the same (and only) form, aForm, to eliminate the page refreshing on each list change. There no longer is an "onChange" modifier. Form paramaters are as follows:

<form action="" method="POST" onSubmit="return CheckForm(this)" name="aForm">  


Submit button that doesn't work:

<input type=submit Value="Submit Order">


I still have the problem of having to "submit the form before I submit it." I added a checkbox to test this:

<INPUT TYPE=CHECKBOX NAME="stupidCrap" onclick="aForm.submit()">SUBMIT<P>


I only get non-null values if I check the box before submitting the form, and that ends up reloading the page. I can't figure out why the form isn't submitted with the "Submit" button before it calls "return CheckForm".
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As of today I've basically changed the way that the pages display so this question isn't as relevant anymore, but I pretty much understand why the way I had it before wouldn't work right. Thanks.