• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

retrieving a created variable in function

the following is my function

function getAccountNumber(clientAccountNumber)
{
alert('function called');
alert(clientAccountNumber);      
}

I need to bring clientAccountNumber as a variable out of the function to use within another function
as soon as I take my second alert out of the function I get an error undefined...

what am I doing wrong or what am I missing ?
0
prowebinteractive
Asked:
prowebinteractive
  • 21
  • 13
  • 3
1 Solution
 
Pravin AsarPrincipal Systems EngineerCommented:
You could use eval ('clientAccountNumber');


0
 
ZvonkoSystems architectCommented:
The var is localy defined because it is only defined as function parameter.
Change it to this:

function getAccountNumber(acc){
  alert('function called');
  clientAccountNumber = acc; // no var prefix to let it be global
  alert(clientAccountNumber);    
}


0
 
Pravin AsarPrincipal Systems EngineerCommented:
Here is an example

<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
     alert (err);
}
onerror = catchErr;
clientAccountNumber = 123456;
alert (eval ('clientAccountNumber'));

// try to get undefined variable will show error
alert (eval ('clientAccountNumber2'));
</script>
</body>
</html>

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ZvonkoSystems architectCommented:
eval() is NOT necessary.
0
 
prowebinteractiveAuthor Commented:
the following is what I have now and Im still geting undefined...

function getAccountNumber(clientAccountNumber)
{
alert('function called');
myClientAccountNumber = clientAccountNumber;

}
alert(myClientAccountNumber);
0
 
ZvonkoSystems architectCommented:
In that sequence is the function NOT called and therefore no global var defined.
Check this:

function getAccountNumber(clientAccountNumber){
  alert('function called');
  myClientAccountNumber = clientAccountNumber;
}
getAccountNumber(123);
alert(myClientAccountNumber);



0
 
prowebinteractiveAuthor Commented:
getAccountNumber(123); cant do that, Im working in a external .js file where Im retrieving a PHP variable in the called function
0
 
ZvonkoSystems architectCommented:
You CANNOT retrieve PHP vars in JavaScript.
0
 
ZvonkoSystems architectCommented:
What you perhaps miss in your tests is that PHP is executed on web server side and produces a text stream which is flushed and sent to browser.
The browser interpretes that text stream and executes the script statements if it is in <script> context.
But you never can access anything on server side from browser side. Only way is the you server modfies the script source before it is loaded in browser. But then you get the browser script cashing problem and need to change the script file URL at every fetch from server.

0
 
Pravin AsarPrincipal Systems EngineerCommented:
Unless PHP is spitting out the Javascript variable, you can not get it

0
 
ZvonkoSystems architectCommented:
That's the short version of my explanation ;-)
0
 
prowebinteractiveAuthor Commented:
this is where I call the function:

src='../../include/webMenu.js' language='JavaScript1.2' type='text/javascript'></script><script type='text/javascript'>awmBuildMenu(); getAccountNumber('<? echo $_GET['accountNumber'] ?>');</script>

now as you could see the source of the javascript is in a file called webMenu.js

in the webmenu.js I have the function getAccountNumber();
0
 
prowebinteractiveAuthor Commented:
well the way I did it Im retrieving the php variable in the js file

this works:
function getAccountNumber(clientAccountNumber)
{
alert('function called');
myClientAccountNumber = clientAccountNumber;
alert(myClientAccountNumber);
}

but this doesnt:

function getAccountNumber(clientAccountNumber)
{
alert('function called');
myClientAccountNumber = clientAccountNumber;

}
alert(myClientAccountNumber);

the variable myClientAccountNumber needs to be used with in the whole .js document so I need to pull myClientAccountNumber out of the function
0
 
ZvonkoSystems architectCommented:
That should work.
But be aware that alert(myClientAccountNumber); will give the passed acc first after the  getAccountNumber('<? echo $_GET['accountNumber'] ?>'); call location on the html page. Before that call is the global var not assigned.

0
 
ZvonkoSystems architectCommented:
Do this:

<script DEFER src='../../include/webMenu.js' language='JavaScript1.2' type='text/javascript'></script><script type='text/javascript'>awmBuildMenu(); getAccountNumber('<? echo $_GET['accountNumber'] ?>');</script>


The DEFER parameter will delay the script source evaluation before the page is completely loaded.

0
 
prowebinteractiveAuthor Commented:
doesnt work

and it doesnt work only when I pull my javascript variables out of the function....
0
 
ZvonkoSystems architectCommented:
And why do you have to have single steents executed at include file evaluation? That does not make any usefull usage that I could imagine. Put that stand alone statements in a function called at page onLoad event handler time.

0
 
prowebinteractiveAuthor Commented:
all right I put in onload instead...   it still gave me the error

the error is not where or how I call the function

its taking the value of the declared variable in the function for use outside the function
0
 
ZvonkoSystems architectCommented:
We can go on turning in cycles before you understand basics of script evaluation sequence.
Let us start from the beginning.
This is plain valid:
<script>
doMyFunc(33);
//
function doMyFunc(theNum){
  alert(theNum);
}
</script>

That mean, you can call a function from a script before it occurrence on the page. The reason is that the source is scanned before it is started to be executed.
But this is not valid:
<script>
alert(myVar);
var myVar=33;
</script>


The source is again scanned before execution, but you cannot use the var before it execution assignment statement is executed.
That is your problem in your script.
You have a function in your included source that assigns the global var. That function can be anywhere in that source before or after the single statements that use the global var assigned in that function. The location of the function on the script page does not matter. But what matter is the time or location when it is executed. That mean, the definition of a function alone does not create the global var. The global var is first created after that function is called and executed. Therefore you cannot use nowhere the global var from that script source because the call and creation of that global var is done by call from outside of that script source, namely in the next script. Do you see that problem?

0
 
prowebinteractiveAuthor Commented:
why do things have to be made so damn complicated
0
 
ZvonkoSystems architectCommented:
:-)

How can we go on solving this? I mean, either you show your online URL and we can analyze what the best approach is or you ask questions. But single lines do not help you out of context.
Does you onLoad approach work now? Can we close this question?

0
 
prowebinteractiveAuthor Commented:
yes onload works.....

but the fact that I cannot retrieve a variable created in a function, is not working
0
 
ZvonkoSystems architectCommented:
The fact is not true. You can access the var created in a function as soon as the function is executed, but not before it is executed. What is your problem?
0
 
prowebinteractiveAuthor Commented:
ok makes sence, however I cannot recall the function without an argument remember I have sent a php variable
0
 
ZvonkoSystems architectCommented:
That is a sequence problem, not a script problem.
0
 
prowebinteractiveAuthor Commented:
ok... what do you need me to show or do and I will do just that this damn thing is taking to long....
0
 
ZvonkoSystems architectCommented:
Is your page accessible online?
0
 
prowebinteractiveAuthor Commented:
it is online, but this online software is for a second chance credit company and there is some info in there that cant be released, the page in question is password protected

I could give you a ling to be able to download the .js file


is that good ?
0
 
ZvonkoSystems architectCommented:
Send me the infos to the email address in my profile and trust my professionalism.
0
 
prowebinteractiveAuthor Commented:
zp  @  arcor . de ?
0
 
ZvonkoSystems architectCommented:
Do not post email addresses in Questions. And yes, but of course without the blanks.

0
 
prowebinteractiveAuthor Commented:
email sent
0
 
ZvonkoSystems architectCommented:
OK, I think we can reduce the complexity with a simple trick.
You need not to pass around the script var.
You have that account var in a hidden field value.
But that is all not neccessar.
Because your menu script needs the parameters which are already in the popup URL you can use the location.search property.
Change this:
it=s1.addItemWithImages(6,7,7,"Applicant",n,n,"",n,n,n,3,3,3,n,n,n,mystring + "clientDetails.php?accountNumber=",n,n,n,mystring + "https://domainname.com/secure/admin/clientDetails.php?accountNumber=",n,0,0,2,6,7,7,8,9,9,1,1,1,0);

To this:
it=s1.addItemWithImages(6,7,7,"Applicant",n,n,"",n,n,n,3,3,3,n,n,n,mystring + "clientDetails.php"+location.search,n,n,n,mystring + "https://domainname.com/secure/admin/clientDetails.php"+location.search,n,0,0,2,6,7,7,8,9,9,1,1,1,0);


But because I got the extra access over email and phone is points assigning to me not allowed by EE rules.
Ask please for points refund here: http:/Community_Support/askQuestion.jsp

0
 
ZvonkoSystems architectCommented:
Sorry, I posted your URL here. I will ask for cleanup on Community Support.
And you can change your webMenu.js not to contain the host name.
Like this:
it=s1.addItemWithImages(6,7,7,"Applicant",n,n,"",n,n,n,3,3,3,n,n,n,mystring + "clientDetails.php"+location.search,n,n,n,mystring + "/secure/admin/clientDetails.php"+location.search,n,0,0,2,6,7,7,8,9,9,1,1,1,0);

0
 
ZvonkoSystems architectCommented:
Thanks for removing the URLs.
I got the userid and password to access a closed user group account. Therefore is the equality rule broken. Please remove the points from this question.
And thanks for that ee-stuff thing! I did not know it. Opening the CV table I wondered why I am in second row with my 600 handled questions. When I sorted I saw that are 600 compared to 68000! from the top CV Venabili questions :)

0
 
ZvonkoSystems architectCommented:
Oh, one more thing. The account and password was not alone the key. Talking on phone to asker I got the inside view what that all was for and could suggest a totally new approach.

0
 
ZvonkoSystems architectCommented:
OK, when you say so :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 21
  • 13
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now