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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting a page variable to a form value 1

Status
Not open for further replies.

giles100

Technical User
Feb 18, 2007
10
GB
Hi

I have a requirement, that I’m sure is simple but it’s foxing me! I have a number of scripts that run in a page head The result is returned to the variable “result”. This all works fine, but I need to pass it now as a form value. Ive tried variants of the example ... but the form seems unable to see the variable. Can you help? Thanks in advance.

<script language="javascript">
var result
if (isinstalled == 2)
{
if (theversion)
result = theversion;
else
// there is a version but can't tell
result = "1";
}
else result= "0";
</script>

</HEAD>
<BODY>


<FORM NAME="flashVer" method="get">
<input name='theResult' type='hidden' value='result'>
<input type="submit">
</FORM>
</BODY>
</HTML>
 
The site has a generic design that uses JS to concate GET commands from a number of forms in a number of pages. In this instance I'm building a page to detect for Flash so I need to get a value from a variable common the the scripts on the page accross to the form to enable it to be passed on.
 
try the following :

<script language="javascript" type="text/javascript">
var result
if (isinstalled == 2)
{
if (theversion)
{
result = theversion;
}
else
{
// there is a version but can't tell
result = "1";
}
}
else
{
result= "0";
}
</script>
</HEAD>
<BODY>
<FORM NAME="flashVer" method="get">
<input name='theResult' type='hidden' value='result' id='result'>
<input type="submit">
</FORM>
<script language="javascript" type="text/javascript">
var theField=document.getElementById("result");
theField.value=result;
</script>
</BODY>
</HTML>

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
giles100, your code is setting a value for the variable result but it in no way set's that value into the form field. The form field of the same name will not automatically get the value of the javascript variable.

Also, since your javascript is not within a function called after the page has loaded, it is being executed AS the page loads. What this means is that even if you had code in your script to set the value of the form field to the value in the javascript variable it would not work because at the time the javscript is executing the form has not yet been created because the page is processed from the top down.

ggriffit has solved the problem by adding javascript at the bottom of the page to take the value of the javascript variable and place it into the value property of the hidden form field. Since the code executes at the end of the page the form field has already been created and therefore can have it's value assigned.

Another approach would be to have your original javascript called as a function after the page has already loaded, either by placing a call to the function at the bottom of the page or by having an onload event in the body tag make the function call and have that function assign the value to the form field.
The method you follow is not as important as understanding the flow of how the code is executed and the page rendered.

ggriffit's solution is perfectly valid though my solution would have been more like this.
Code:
<script language="javascript">
[COLOR=red]function setResult() {[/color]
  var result
  if (isinstalled == 2)
  {
    if (theversion)
      result = theversion;
    else
      // there is a version but can't tell
      result = "1";
  }
  else result= "0";
[COLOR=red]  document.getElementById('theResult').value = result;
}[/color]
</script>
</HEAD>
<BODY[COLOR=red] onload="setResult()"[/color]>
<FORM NAME="flashVer" method="get">
    <input name='theResult' [COLOR=red]id="theResult"[/color] type='hidden' value=''>
    <input type="submit">
</FORM>
</BODY>
</HTML>

NOTE: The code is incomplete as it relies on other values you reference but did not show in your code. This is only to show you the method.

At my age I still learn something new every day, but I forget two others.
 
Hey folks, thanks for taking the time to answer my question. They are both great ... and educational. I could see where I was going wrong, but couldn't make the connection. Thanks again.

Giles
 
niteowl,
thanks for cleaning up my code into a proper function, should have done that myself, but was replying to the post from the server room at about midnight local time, so a little braindead ;-) have a star.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top