Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question
    
Are $var1 and $var2 strings or integers? what are those "absurd errors" you get? – Damien Pirsy Apr 15 at 7:18
    
absurd errors? Give us an example of those... – RGraham Apr 15 at 7:19
    
What are absurd errors and what does the generated html / javascript source code look like? – jeroen Apr 15 at 7:19
    
Short tags may not be enabled on the server you're working with. You do actually need spaces between the opening short tag and the variable. – Andrew Apr 15 at 7:20
    
As i know.. We cannot use PHP code in JavaScript code.. – phpfresher Apr 15 at 7:20

1 Answer 1

HTML with PHP

<button type="button" id="btn1" onClick="callJavascriptFunction('<?=$var1?>','<?=$var1?>');">Text of button
</button>

Javascript:

<script>
function callJavascriptFunction(var1,var2) {
 alert(var1); alert(var2); 
}
</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.