Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing a value from Javascript to JSP  RSS feed

 
rita mistry
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can anyone tell me how to pass a value that is stored in a Javascript var to a JSP variable.
I can't use

because the form has not been submitted yet.
Any help will be very much appreciated.
Many Thanks,
Rita.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JSP Forum.
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me make sure I understand your question.
You have a form which assigns a value to a Javascript variable ... you havn't submitted the form ... and you want that value? Correct?
 
rita mistry
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
Yes that is correct.
Let me explain a bit more...
I have a form which has a drop down for a list of companies. Each company has a certain number of projects assigned to it. So what I am trying to do is when the user selects a company, on the projects drop down below a list of all the projects belonging to this particular company will be displayed.
I am using a Javabean to get the items from the database, but I need to pass the information of which company it is in the method parameter.
I don't know how to do this so I thought I could store it in a Javascript var and then pass the value to a jsp var which then means I can use it for the javabean.
I hope this makes sense. If you think there is a better way of doing this then I'm all ears.
Kind Regards,
Rita.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this thread for Bill Brogden's reply to a similar question: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=50&t=000502
Since you're already using JavaScript, you could use a JavaScript event such as onclick, onchange or onsubmit to set the value of a hidden form field. For example:


or something like that
 
rita mistry
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu,
Correct me if I am wrong but doesn't the code that you have written above, just store the value in a javascript variable.
I have already obtained the value I need from the form, but I need to now store it in a jsp variable. How do I do this?
If the code above does this then I do apologise.
I'll have a closer look at it.
Kind regards,
Rita.
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the link he gave too!! It would seem it's not possible to do this without submitting the form!
 
rita mistry
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help Angela and Junilu.
If I find a way around the problem without having to submit I'll send a post.
Thanks again
Kind regards
Rita.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh! What you want can be done, and I did something very similar to it just last week, only your requirements are a bit more complicated. I assume that your list companies is pretty static while the list of projects can grow or shrink from time to time, right?
You'll have to do some funky JavaScript/JSP to get this done the way you want it.
I'll post some code in a bit... (need to modify some things to protect the innocent )
 
rita mistry
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu,
The items in the company and project lists are outputted from the database. So both the company and project list will grow and shrink but in the case of the company list it won't be that often.
I really appreciate your help. This coding is driving me nuts. Thanks very much.
Rita.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rita,
here's some code you can try to work with. With some modifications, you can get it to satisfy your requirements.
Good luck!

(Edited for cut-and-paste errors )
[ January 30, 2002: Message edited by: Junilu Lacar ]
 
rita mistry
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu,
Your a Star.
Kind regards,
Rita.
 
elijah bitt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar problem to the one discussed here. I am using a JSP bean in a javascript fn. This is no problem, but I need to pass a value from my HTML form to the Bean's method call. I have the value in the javascript but can't for the life of me figure out how to make the javascript variable available to the JAVA call. The j.s. function follows ('client' is the bean name)
function x(){
//get value from textbox
var t_val = document.forms[0].rec_text.value;
if(t_val=="") return;
<%
client.recommendReceiver(t_val);
%>
document.forms[0].rec_text.value = "";
}
The call works fine if I hard-code a value where <t_val> is...The prblem is t_val is a js variable and not recognized by JAVA.....I have lloked everywhere and found nothing -
PLAESE HELP!
-Elijah
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!