2

I've a percent variable in my javascript, that variables is passing result from PHP.

This is the javascript:

test.js

console.log(percent); //* //variable was passed from PHP

function displayLoading() {
    console.log(percent); //**
}

If I use *console.log(percent) out the function it will print_out the value of percent in console. But if I use **console.log(percent) inside the displayLoading function, it will print_out as undefined.

How I can access the outside variable inside a function?

I've tried this way

from stackoverflow

var funcOne = function() {
    this.sharedVal = percent;
};
var funcTwo = function() {
    console.log(funcOne.sharedVal);
};

and give print_out undefined into console log.

and

from stackoverflow

var per = percents
console.log(per);   //this line print_out the value into console log

function displayLoading() {
   console.log(per);    //this print_out "undefined" into console log.
   var myPercent = per;
   console.log(per);    //and also print_out "undefined" into console log.
}

Both of code above didn't work for me, any one know another way? Any help is appreciated, thanks :)

EDITED:

The percents inside javascript above, I get from this code:

headerController.php

<?php
   $percent = $percent + 10;
?> 
    <script type="text/javascript">
       var percents = <?= $percent; ?>;
    </script>
    <script type="text/javascript" src="../../web/js/test.js"></script>

The main problem has found, the reason why I got undefined is because I print_out the percents right before the variable has passed from php.

How do I can pass php variable directly into a function in javascript file (test.js in this case)?

7
  • in which statement you are asigning value in percent? Commented Jan 17, 2017 at 5:30
  • Maybe, you're assigning the variable locally, try assigning it outside then use console.log(window.percent); inside the function Commented Jan 17, 2017 at 5:32
  • I've tried this way - except you didn't, not really Commented Jan 17, 2017 at 5:34
  • this little code snippet is not enough to determine the problem. If percent is a global var (can't see how it's being defined, so who knows) then the very first code snippet should work fine Commented Jan 17, 2017 at 5:36
  • I'm sorry about the code, I have update my question. Check it :) Thanks Commented Jan 17, 2017 at 6:34

5 Answers 5

0

Define a global variable.

percent = 'foobar';
// window.percent = 'foobar'; /* same thing */

function showthing() {
  console.log(percent);
}

showthing();

Sign up to request clarification or add additional context in comments.

1 Comment

Yah, thanks. I've try and it's work. But I make a mistake, the problem cause is I call the function when the php variable not passed yet to javascript.
0

try removing var from var per = percents. like this

per = percents
console.log(per);   //this line print_out the value into console log

function displayLoading() {
   console.log(per);    
   var myPercent = per;
   console.log(per);    
}

2 Comments

Why you declare myPercent?
that's not needed. i just copied and your code . all thing you have to do is change var per = percents to per = percents
0

I don't know which console (browser or JS REPL - however they are almost same) are you talking about, but REPL always outputs the return value of a function. I don't think it's about global/local scope, as it's stated above.

>> p = 5; function dl() { console.log(p); }
<- 5
>> dl()
   5 
<- undefined
>> foo = function() { return "foo" }
<- function foo()
>> foo()
<- "foo"

2 Comments

I'm using mozilla log console, that is REPL console result above?
this is also from mozilla's web console from developer tools.
0

A variable:

1) Declared with var keyword has local scope where it is declared.(if you declared it outside function you can not use it inside function and vice versa.

Example:

   var  percent = 'foobar';
    function showthing() {
      console.log(percent);//wrong
    }
    console.log(percent);//correct

2) Declared within particular function has local scope within function.

Example:

     function showthing() {
     var  percent = 'foobar';
      console.log(percent);//correct
    }
console.log(percent);//wrong

1 Comment

Thanks for the knowledge :)
0

Is it a problem with the way you pass the variable from PHP, like AJAX? When you call displayLoading() the AJAX call may not have answered yet.

4 Comments

Yeah, you're right :D. I call the function when the variable not passed yet. May be you know how I can pass directly into a function in javascript?
You could do something like this if you use jQuery: $.get( "ajax/test.php", function( data ) { displayLoading(); });
Thanks, I'm just learn about javascript / jquery. May I know exactly where I've tu put this code? inside php controller code with <script>....</script> or in my test.js?
In your javascript file. But my suggestion is to rather change the order of lines than adding new code. In the block of code where you do the AJAX, after you get the value for your variable, call that function displayLoading. If you wish to simply combine JS with PHP: Your HTML file should be a PHP file, right? So you write in the header or footer of HTML <script>myVar = <?php echo $anotherVar; ?>;</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.