Solved

Passing Form textbox value as a global varibale to be used by multiple javascript function

Posted on 2012-03-21
8
204 Views
Last Modified: 2012-12-08
I couldn't get it going.. I have input textbox, want to pass the value (userid) as a global varibale, so I can use them as a seed for my random function generator then I can use them in multiple javascripts function.

I am able to pass it as a local varible but couldn't get it out as global varible. Can you help?



<script type="text/javascript">
                  
function seeduserid (form)
      {
            var seedvar = form.userid.value;
            //var mrnot = new MersenneTwister(seedvar);
            //alert (mrnot.random());
            //alert (seedvar);
            return(seedvar);
      }
      
      //var mrnot = new MersenneTwister(100)
      var mrnot = new MersenneTwister(seedvar)
      
      
      var x = mrnot.random()*451;;
      & other varibales and fuctions....
      
<SCRIPT>

<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="transForm">
<p> User ID: <INPUT TYPE="text" NAME="userid" id="userid" VALUE="" onblur="seeduserid(this.form)">
</FORM>
0
Comment
Question by:mkanagar
  • 4
  • 3
8 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 300 total points
ID: 37747751
To be global he var must be declared outside of a function.

do it this way:
<script type="text/javascript">
var seedvar;
                 
function seeduserid (form)
      {
            seedvar = form.userid.value;
            //var mrnot = new MersenneTwister(seedvar);
            //alert (mrnot.random());
            //alert (seedvar);
            return(seedvar);
      }



Cd&
0
 

Author Comment

by:mkanagar
ID: 37747952
I understand it has to be outside the function, but what I want is the global variable update it selelf to the value returned form the function.

function seeduserid (form)
      {
            seedvar = form.userid.value;
            //var mrnot = new MersenneTwister(seedvar);
            //alert (mrnot.random());
            //alert (seedvar);
            return(seedvar);
      }

var seedvar1 = seeduserid();

var mrnot = new MersenneTwister(seedvar1)


It still not settting the global variable "seedvar1" to "userid"
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37748240
You are calling seeduserid with no parameter.  That results in a null value being returned because form.userid.value is undefined.


Cd&
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mkanagar
ID: 37748268
what parameter I should call seeduserid with to make the "seedvar" as a global variable
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37748402
It becomes a global by being created outside of any function.  It mus exist before you reference it in a function.

the problem I was referring to was: var seedvar1 = seeduserid();  which cannot possibly return a value without a formname for a parameter.

The proper way to do it is to give the form an id and reference the id instead of this.form you could then use document.getElementById('idofform')


Cd&
0
 

Author Comment

by:mkanagar
ID: 38672576
In March I have completed the task but forgot exactly how I did it now.

However, for this particular question, what COBOLdinosaur (Posted on 2012-03-21 at 14:57:54ID: 3774775) suggested  "To be global he var must be declared outside of a function. " did work. Closing this question.
0
 

Author Closing Comment

by:mkanagar
ID: 38672581
Didn't get the exact answer that I needed but "To be global he var must be declared outside of a function." did help.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to dynamically set the form action using jQuery.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now