-1

I am trying to bring a php variable into my javascript code to use for a countdown timer. Because of the other functions happening based on the timestamp php variable, I need to to be in php as well.

<?php  $access = '1443907640'; ?>
<script>
    $(function() {
        var access = <?php echo $access ?>;
        var note = $('.note'),

        ts = (new Date(access * 1000)).getTime() + 1 * 24 * 60 * 60 * 1000;


        $('.countdown').countdown({
        timestamp: ts,
        callback: function(days, hours, minutes, seconds) {

            var message = "";

            message += days + "<small class='opacity'>D</small>, ";
            message += hours + "<small class='opacity'>H</small>, ";
            message += minutes + "<small class='opacity'>M</small>, ";
            message += seconds + "<small class='opacity'>S</small>";

            note.html(message);

        }


        });
    });
</script>   

Then I call call it with html here but doesn't work

<div class="note"></div>
11
  • What errors are you seeing in the js console? Commented Oct 3, 2015 at 21:38
  • Since you use Jquery, do you call it from within $(document).ready()? Commented Oct 3, 2015 at 21:39
  • Not seeing errors in console. I am mainly a php guy, not very good with js and jquery. I am only trying to use it so the countdown timer counts down by the second and isn't static Commented Oct 3, 2015 at 21:43
  • Just tried that solution, now seeing this in console ReferenceError: $ is not defined Commented Oct 3, 2015 at 21:45
  • Can you post how you call this function? Commented Oct 3, 2015 at 21:47

2 Answers 2

1

I think the real problem in your code is in the end of this line :

var note = $('.note'),

Replace , by ; and it should work.

NOTE : line var access = <?php echo $access ?>; work fine in my test.


To solve the following error :

TypeError: $(...).countdown is not a function

You should add jquery.countdown script :

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.countdown/2.1.0/jquery.countdown.js"></script>

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Really? I tried that and am getting TypeError: $(...).countdown is not a function
You can see that i talk about a line and not the whole code... to solve the previous error see my updated answer.
Ok did that, made the error disappear but still blank massblogprofits.com/get-launched/test.php
Ok, i cant see the class countdown in your code so please try to follow some tuto in jquery.countdown for a better implementation.
Your solution plus forgetting to load my plugin, work now. Thanks!
0

Maybe consider to put an semicolon after your PHP-echo.

<?php echo $access; ?>

or the short-and-dirty version:

<?=$access?>

in line 4:

var access = <?=$access?>;

or

var access = '<?=$access?>'; //if $access would be typeof string

4 Comments

4th line in his code, isn't it?
I have it as var access = "<?php echo $access; ?>";
yes! And you normally can omit the 'php' after those <? . I edited my post to make this clear.
I cannot comment the other Answer: If your page is blank, you have an PHP-Error somewhere.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.