在PHP中调用Javascript函数,同时将PHP变量传递给函数 [英] Calling Javascript function in PHP while passing PHP variables into the function

查看:133
本文介绍了在PHP中调用Javascript函数,同时将PHP变量传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP代码中调用JavaScript函数并尝试将我的PHP变量作为参数传递给函数时遇到问题。虽然看起来对我来说很简单,但我无法通过尝试不同的语法来找到它。

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>';
?>

如果我尝试传递常量(例如123),函数被调用并且值被传递,但是当试图传递PHP变量时,函数根本不会被调用。

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.

如何正确执行此操作?

单引号将你的 $ var 设置为

解决方案

推荐答案

> $变种。双引号将 $ var 放入 0 1或2


$ b $

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

这篇关于在PHP中调用Javascript函数,同时将PHP变量传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆