Reputation: 99
I have the following jquery script:
<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>
And i'm trying to pass a php variable like so:
<?php
$gdb->connettiDB();
$row = $gdb->getFotoProfilo(getId());
?>
var phpvariable=<? echo $row; ?>
That 's not working. When i declare a var inside the script, everything seems blocked and my animations aren't working and I don't know why. Can anyone explain me how can I exactly do that?
Upvotes: 0
Views: 1182
Reputation: 91734
The correct way to pass variables from php to javascript, is to use json_encode
:
var phpvariable=<?php echo json_encode($row); ?>;
Now the php will not accidentally break your javascript regardless of the type of variable that is $row
. And the name suggests something like an array...
If $row
is an integer, the problem could also have been caused by a missing short-tag setting in php. That's why I have used <?php
in my example.
Lastly, a missing ;
after your variable declaration in javascript, could also cause problems but that would depend on what comes after it.
Upvotes: 1
Reputation: 3491
You need to make sure the html containing the <script>
you've posted above is in the same file. The $row
variable needs to be defined before the <script>
.
You are also missing a ;
at the end of the javascript line
var phpvariable=<? echo $row; ?>;
If $row
is a string, you also need to use "
:
var phpvariable="<? echo $row; ?>";
Upvotes: 0
Reputation: 22532
Wrong use of php:-
var phpvariable=<? echo $row; ?>
It should be
var your_variable='<?php echo $row; ?>';
Upvotes: 1