Following:
I read a lot of posts on different site, but actually nothing is working.
I'm trying to pass a PHP-Variable to Javascript, either in the function call as parameter or define it in the function itself (I don't care). But it's not working.
I have two global variables defined with:
public $var1;
public $var2;
What I have then is a button calling a function:
<button type="button" id="btn1" onClick="callJavascriptFunction();">Text of button
</button>
And my JS-Function defined with:
function callJavascriptFuncion(){}
What I tried are the following things:
Before the button, having:
global $var1;
global $var2;
And then calling the function with callJavascriptFunction(<?=$var1;?>,<?=$var2;?>);
(having the parameteres defined in the definition of the function as well).
-->Not Working
Then I tried something like doing this in the function:
function callJavascriptFuncion(){
<?php global $var1; global $var2;?> //Also tried without this line...
var variable1 = <?=$var1;?>;
var variable2 = <?=$var2;?>;
}
-->Not working, getting absurd errors..
What I tried last is having this in the function:
function callJavascriptFuncion(){
var test = "<?php echo 'test';?>";
alert(test);
}
--> Even this is not working. Does anybody know why and how it works instead?
Thank you.
Edit
When e.g. trying the last thing, there are errors coming up, having (in my opinion) nothing to do with it, but only when I have those to lines included.
Following errors: Uncaught SyntaxError: Unexpected token ILLEGAL
(on an empty line when klicking on the error) and: Uncaught ReferenceError: takeapic is not defined
in a line that actually worked before...
Also f.y.i: I'm working in a php class, does this change anything?
And it's a pure php file, including some JS.
The output looks like:
function writeqrcodecontentText(){
text = 'test';
alert(text);
}
Passing the variables in the function seems to work now, didn't use the quotes (''
) before.
Even if I can't understand why variables are working now, but not echoing simple strings, it seems to work now whith the variables. Thank you.