将php变量传递给jquery脚本无法正常工作

我有以下jquery脚本:

 $(document).ready(function () { $(".clickable").click(function () { $(this).animate({left: '1030px'}, function () { $(this).fadeOut("slow", function () { document.location.href = $(this).get(0).id + ".php"; }); }); }); }); $(document).ready(function () { $("#Chi").animate({left: '0'}, {duration: 200, queue: false}); $("#Dove").animate({left: '0'}, {duration: 400, queue: false}); $("#Quando").animate({left: '0'}, {duration: 600, queue: false}); $("#Cosa").animate({left: '0'}, {duration: 800, queue: false}); var phpvariable= $('.dreams-photo-profile-mydream').css({"background": "url(\"/images/user.png\")"}); $('.dreams-photo-profile-mydream').css({"background-repeat": "no-repeat"}); $('.dreams-photo-profile-mydream').css({"background-position": "center"}); $('.dreams-photo-profile-mydream').css({"background-size": "contain"}); });  

而我正在尝试传递这样的php变量:

  connettiDB(); $row = $gdb->getFotoProfilo(getId()); ?> var phpvariable= 

那不行。 当我在脚本中声明一个var时,一切似乎都被阻止了,我的动画也没有用,我不知道为什么。 任何人都可以解释我怎么能完全这样做?

错误使用php: –

 var phpvariable= 

它应该是

  var your_variable=''; 

将变量从php传递到javascript的正确方法是使用json_encode

 var phpvariable=; 

现在php不会意外地破坏你的javascript而不管$row的变量类型。 这个名字暗示了一个像数组……

如果$row是一个整数,问题也可能是由于php中缺少短标签设置引起的。 这就是我在我的例子中使用的原因。

最后,一个失踪; 在javascript中进行变量声明后,也可能会导致问题,但这取决于之后的内容。

您需要确保包含您上面发布的的html位于同一个文件中。 $row变量需要在之前定义。

你也错过了; 在javascript行的末尾

 var phpvariable=; 

如果$row是一个字符串,你还需要使用"

 var phpvariable="";