Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning value to JSP variable in a JavaScript 1

Status
Not open for further replies.

blueindian1

Programmer
Apr 24, 2001
150
US
Hi,

Is it possible to assign a value to a JSP variable with a script? I want to do something like the following:

Code:
<% String str = &quot;jsp&quot;; %>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
if (document.all) {
  <%str = &quot;modifed_by_the_script&quot;; // assignment only when condition is met%>
  alert(&quot;Internet Explorer Detected&quot;);
}
// -->
</SCRIPT>

The way I have it, the string has a new value assigned regardless of whether the &quot;if&quot; condition is met

Thanks,

Rich
 
Well, since the JS is client-side and the JSP is effectively dead at that point, no, you can't do it the way you are trying to do it... you could dynamically assign a form variable with the script though and reload the page then pull that out of the http response...

something like:
<%
String var1 = request.getParameter(&quot;variable1&quot;);
%>


<script language=&quot;javascript&quot;>
function submitIt(){
document.forms[0].variable1.value = whatever you want to set
document.forms[0].submit();
}
<script>

<body onload=&quot;submitIt();&quot;>
<form name=&quot;variables&quot; action =&quot;thispage.jsp&quot;>
<input type=&quot;hidden&quot; name=&quot;variable1&quot; value=&quot;&quot;>
</form>

See what I'm saying? It's ugly, but that's the only way I can think of to get the client to write jsp... actually you would want to build the body tag based on the value of the variable, so you don't get into a loop (if it's null, call the script...)

-Scott
 
thanks!

as best i can tell...that is the only way to do it. i'm working on that now....if i ever figure out a differnt way i'll be sure to let the world know

:)
 
Hi,

I'm trying to do the same as above (set a form variable value in a JavaScript client-side function) and when I run the following line (mentioned above)

document.forms[0].variable1.value = whatever you want to set

I get an error that says:

Error: document.forms[0].variable1.value is null or not an object.

Any ideas?

Thanks,
Ray
 
wrbodine,

you have to post the form, or you form variable will be null.
 
blueindian1,

How would I post the form? Here's my code more accurately that I'm using:

<head>

<script language=&quot;JavaScript&quot;)

function CheckValues(cond, value) {

...
if (cond == &quot;end&quot;) {
document.MyForm.vari.value = value;
document.MyForm.Submit();
}

// 3 other if conditions (not really necessary, but how I was doing it at first...)

}

</script>

<body>
<form ACTION=&quot;DailyJumboForm.asp&quot; METHOD=&quot;POST&quot; name=&quot;MyForm&quot;>

...
<input type=&quot;hidden&quot; name=&quot;vari&quot; value=&quot;<%=strVari%>&quot;>

<input type=&quot;image&quot; src=&quot;../../../images/btnForwardArrowLine.gif&quot; onClick=&quot;Javascript:Arrows('end', 'endValue');&quot;>

3 other images

...
</form>


I tried adding document.MyForm.method = &quot;Post&quot; before setting the value in the client-side function, in case that's what you were talking about, but that didn't do it.

Thanks so much,
Ray
 
it looks to me like you are calling the wrong function. you function is name CheckValues but your onclick is calling Arrows
 
My mistake in posting this; the actual one is called Arrows, and it does have the correct name in both places; I typed this code out instead of just copying it...

Thanks,
Ray
 
...
if (cond == &quot;end&quot;) {
document.MyForm.vari.value = value;
document.MyForm.Submit();
}

i think you need to submit the form before you set the value, but i'm no javascript whiz....by any means

...
if (cond == &quot;end&quot;) {

document.MyForm.Submit();
document.MyForm.vari.value = value;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top