Click to See Complete Forum and Search --> : [RESOLVED] Pass value from Javascropt to JSP


belebala
June 1st, 2009, 04:11 PM
Hello, I'm having a problem to pass an input box value a variable.

I have an index page which contains the top and bottom frames. The top frame contains a combo box and I would like this selected combo box value passes to the bottom frame.

Top Frame JSP:

<html>
...

<script language="JavaScript">
function getProvinceValue(id){
var strGetProv=document.getElementById(id);
var strFilterProv = strGetProv.options[strGetProv.selectedIndex].value;
document.getElementById("hidProvFilter").value = strFilterProv;
parent.BottomFrame.document.getElementById("hidProvFilter").value = strFilterProv;
}
</script>
...
<body>
...
<td><select id ="ProvFilter" onchange="getProvinceValue('ProvFilter')">
<option value="British Columbia">BC</option>
<option value="Alberta">AB</option>
<option value="Saskatchewan">SK</option>
....
<form id="Toolbar" action ="topFrame.jsp" method ="POST">
<%
out.println("<input name=\"ProvinceFilter\" id=\"hidProvFilter\" value =\"Canada\">");
%>
...
</html>


Bottom Frame JSP:

<html>
<body>
...
<%
out.println("<input name=\"ProvinceFilter\" id=\"hidProvFilter\" value =\"\">");

String strProv = ??
//only print the filtered province
%>
...
</html>


From the above topFrame.jsp, it is already passed the strFilterProv to the bottomFrame.jsp, but how can this value passes to a java code (strProv) which I can do a filter?

PeejAvery
June 1st, 2009, 05:25 PM
The only way to pass client-side data to the server-server side is to use GET or POST. Now, whether that is through a form, AJAX, or just through the URL itself, that's up to you to decide.

For example, if you were to pass a URL variable, you could just use request.getParameter("variable") on the JSP side to retrieve it.

belebala
June 2nd, 2009, 10:25 AM
Do you have an example how to pass it through to the URL? Thanks.

PeejAvery
June 2nd, 2009, 10:28 AM
Have you never used URL variables before?

http://www.example.com/page.jsp?variable=value

belebala
June 2nd, 2009, 01:13 PM
I got it working now by just adding parent.BottomFrame.document.getElementById("hidProvFilter").submit() to the getProvinceValue function.