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

I'm experiencing a problem when calling a JavaScript function inside PHP code and trying to pass my PHP variables as parameters into the function. While it seems really simple to me, I can't figure it out by trying different syntax.

Here is a sample code which can demonstrate the problem:

<?PHP
    $var1 = 0;
    $var2 = 1;
    $var3 = 2;
    echo '<script type="text/javascript">functionName($var1, $var2, $var3);</script>';
?>

If I try to pass constants (e.g. "123") the function gets called and the value is passed, but when trying to pass the PHP variable, the function doesn't get called at all.

How can I do this correctly?

share|improve this question
2  
echo "<script type='text/javascript'>functionName($var1, $var2, $var3);</script>"; – aldrin27 Sep 19 '15 at 9:05
    
Your I've change your quotes. – aldrin27 Sep 19 '15 at 9:06
    
That was the actual quotes I was using in my code, does it have any difference? – Ramtin Soltani Sep 19 '15 at 9:07
1  
Yes because single quotes litterally puts your $var as $var. While double quotes puts your $var in 0 1 or 2 – aldrin27 Sep 19 '15 at 9:08
    
I'll make this my answer. Can I? And mark that as green check? – aldrin27 Sep 19 '15 at 9:09
up vote 2 down vote accepted

Single quotes litterally puts your $var as $var. While double quotes puts your $var in 0 1 or 2

echo "<script type='text/javascript'>functionName('$var1', '$var2', '$var3');</script>"
share|improve this answer

I think you should use curly braces to better distinguish variables in a string. Just wrap them like this:

echo "<script type='text/javascript'>functionName({$var1}, {$var2}, {$var3});</script>"
share|improve this answer
    
Thanks. I have a question if you don't mind. When one of my variable contains new lines, the function doesn't get called. I tried nl2br() but it didn't help. Any ideas? – Ramtin Soltani Sep 19 '15 at 10:16
    
Can you post the full source somewhere? – 1000Gbps Sep 19 '15 at 19:39
    
the full source is unnecessary because it's too big. I actually solved the problem by replacing all the newlines with <br/>. To demonstrate the problem, just define a variable in php, and set its value to a textarea's text value (and make sure you have newlines typed in the textarea). Then try to pass that variable to a javascript function. You'll see that the function would never be called. Try the same thing with a textarea without any newlines and it works perfectly. Once again I have already solved this but I don't get the logic of the function never getting called. – Ramtin Soltani Sep 20 '15 at 2:58
    
Strange, nl2br() works for me ... Can you give at least the text which contains newline special characters? – 1000Gbps Sep 20 '15 at 11:38
    
The text varies based on user input. It's just simply any text containing a line break. – Ramtin Soltani Sep 23 '15 at 5:21

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.