aspose file tools
The moose likes JSP and the fly likes Passing a value from Javascript to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Passing a value from Javascript to JSP" Watch "Passing a value from Javascript to JSP" New topic
Author

Passing a value from Javascript to JSP

rita mistry
Greenhorn

Joined: Dec 04, 2001
Posts: 27
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

Joined: Oct 25, 2000
Posts: 7292

Moving to the JSP Forum.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
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?


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
rita mistry
Greenhorn

Joined: Dec 04, 2001
Posts: 27
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
Bartender

Joined: Feb 26, 2001
Posts: 4133
    
    2

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


Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
rita mistry
Greenhorn

Joined: Dec 04, 2001
Posts: 27
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

Joined: Mar 02, 2000
Posts: 3143
Look at the link he gave too!! It would seem it's not possible to do this without submitting the form!
rita mistry
Greenhorn

Joined: Dec 04, 2001
Posts: 27
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
Bartender

Joined: Feb 26, 2001
Posts: 4133
    
    2

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

Joined: Dec 04, 2001
Posts: 27
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
Bartender

Joined: Feb 26, 2001
Posts: 4133
    
    2

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

Joined: Dec 04, 2001
Posts: 27
Thanks Junilu,
Your a Star.
Kind regards,
Rita.
elijah bitt
Greenhorn

Joined: Jul 18, 2003
Posts: 1
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Passing a value from Javascript to JSP
 
Similar Threads
how can i get a value in java script function from database
How to access javascript variable of one file in another jsp file
JavaScript within JSP
how can i put java script value into jsp variable?
Assign Javascript value to JSP var