Javascript 将php变量传递给jquery脚本不起作用
我有以下jquery脚本:Javascript 将php变量传递给jquery脚本不起作用,javascript,php,jquery,Javascript,Php,Jquery,我有以下jquery脚本: <script> $(document).ready(function () { $(".clickable").click(function () { $(this).animate({left: '1030px'}, function () { $(this).fadeOut("slow", function () {
<script>
$(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=<? echo $row; ?>
$('.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"});
});
</script>
$(文档).ready(函数(){
$(“.clickable”)。单击(函数(){
$(this).animate({left:'1030px'},函数(){
$(此).fadeOut(“慢”,函数(){
document.location.href=$(this.get(0).id+“.php”;
});
});
});
});
$(文档).ready(函数(){
$(“#Chi”).animate({left:'0'},{duration:200,queue:false});
$(“#鸽”).animate({left:'0'},{duration:400,queue:false});
$(“#Quando”).animate({left:'0'},{duration:600,queue:false});
$(“#Cosa”).animate({left:'0'},{duration:800,queue:false});
可变变数=
$('.dreams照片配置文件mydream').css({“背景”:“url(\”/images/user.png\”)));
$('.dreams照片配置文件mydream').css({“背景重复”:“无重复”});
$('.dreams照片配置文件mydream').css({“背景位置”:“中心”});
$('.dreams照片配置文件mydream').css({“背景大小”:“包含”});
});
我试图传递一个php变量,如下所示:
<?php
$gdb->connettiDB();
$row = $gdb->getFotoProfilo(getId());
?>
var phpvariable=<? echo $row; ?>
可变变数=
那不行。当我在脚本中声明一个var时,一切都似乎被阻止了,我的动画也不起作用,我不知道为什么。有人能解释一下我到底是如何做到的吗?php的错误使用:-
var phpvariable=<? echo $row; ?>
var phpvariable=
应该是
var your_variable='<?php echo $row; ?>';
var-your\u变量=“”;
您需要确保包含上面发布的
的html位于同一文件中。需要在
之前定义$row
变量
您还缺少一个代码>位于javascript行的末尾
var phpvariable=<? echo $row; ?>;
将变量从php传递到javascript的正确方法是使用:
var phpvariable=;
现在,无论变量类型是$row
,php都不会意外地破坏javascript。这个名字意味着一个数组
如果$row
是一个整数,那么问题也可能是由php中缺少短标记设置引起的。这就是我使用的原因
在?>
@Barmar之前是可选的,但我的意思是在JS行的末尾-为clarityJavascript编辑的代码在必要时也会在行的末尾插入分号。请注意,我并不建议省略分号,只是说这不是问题的原因。@Barmar指出,但这并不完全是一种安全的做法,我们不妨提倡一种好的编码样式。这取决于$row
是字符串还是数字。您不知道getFotoProfilo()是什么
函数返回。你可以构建一个函数,你能告诉我们你的getFotoProfilo()
函数是什么吗?
var phpvariable="<? echo $row; ?>";
var phpvariable=<?php echo json_encode($row); ?>;