Solved

Correct syntax to insert Java variable into HTML?

Posted on 2010-09-02
6
515 Views
Last Modified: 2013-11-24
if I define a Java variable "num" in a function in the header of an HTML page, how can I pass the value of that variable in a form on the same page?

example:

<form method="POST" action="mailscript.html">
    <input type="hidden" name="java_var" value="num">
    <input class="button" type="submit" value="Submit">
</form>

what is the correct syntax to insert the java variable into the HTML?
0
Comment
Question by:ob1_
6 Comments
 
LVL 3

Expert Comment

by:alanpetersen
ID: 33588892
I'm assuming that when you say you are creating an HTML page, you really mean a JSP page. In that case, you could print the value of "num" in the input element like this:

  <input type="hidden" name="java_var" value="<%= num %>" />

If you were using JSTL, you could also use Expression Language (EL) to get the variable (and make things more readable):

  <input type="hidden" name="java_var" value="${num}" />
0
 
LVL 6

Author Comment

by:ob1_
ID: 33588924
no it is an HTML page, what is JSP?
could i use this:
 
 

<input type="hidden" name="numberof" value="<script type="text/javascript">document.write(num);</script>"

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33588952
then this num is a javscript variable.
When you said 'Java Variable' it created the confusion.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 3

Accepted Solution

by:
alanpetersen earned 250 total points
ID: 33589009
Oh, so you are using Javascript, not Java. In your question you said you were creating a Java variable. Java != Javascript

In any case, in a Javascript function, you can set the value of a field like this:
  document.getElementById('java_var').value = num

0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 250 total points
ID: 33589197
<input type="hidden" name="java_var" value="num">
you should use an id attribute

<input type="hidden" id="java_var" name="java_var">

then the usage of
document.getElementById('java_var').value = num;
will be proper.


0
 
LVL 6

Author Comment

by:ob1_
ID: 33589292
wow i didn't even think there would be a different between java and javascript i guess that makes sense - thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use question mark (?) in filename with html 25 73
Hide Table in merge 3 34
HTML <font style="color:red"> 9 33
CSS DIV Height in Percent 1 9
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will learn how to implement Singleton Design Pattern in Java.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question