Solved

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

Posted on 2012-03-21
8
210 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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