0

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?

1
  • Can you show us what does your getFotoProfilo() function makes? Commented Apr 25, 2015 at 11:44

3 Answers 3

1

Wrong use of php:-

var phpvariable=<? echo $row; ?>

It should be

 var your_variable='<?php echo $row; ?>';
Sign up to request clarification or add additional context in comments.

3 Comments

It depends on whether $row is a string or number.
You don't know what getFotoProfilo() function returns.
You could build a <input type="hidden" name="myRow" value="<?php echo $row ?>"> and reference that later in the js.
1

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.

Comments

0

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

8 Comments

; is optional before ?>
@Barmar sure, but I meant at the end of the JS line - edited for clarity
Javascript will also insert semicolons at the end of line when necessary. codecademy.com/blog/78-your-guide-to-semicolons-in-javascript
Note that I'm not recommending leaving out the semicolon, just saying that it's not the cause of the problem.
@Barmar Point taken, but it's not exactly a safe practice, and we may as well advocate a good coding style
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.