NetStack
NetStack

Reputation: 208

Getting end date via mysql in a jQuery countdown

so I have this jQuery script which I'm using for a jQuery countdown.

<script>
$(function () {
    var austDay = new Date();
    austDay = new Date(austDay.getFullYear() + 1, 1 - 1, 26);
    $('#defaultCountdown').countdown({until: austDay});
    $('#year').text(austDay.getFullYear());
});
</script>

But the date here is hardcoded. What I want is, that I want to get the end time from a mysql database and then use it in jQuery. How would I do that ?

Here's my database structure

Product ID           ProductName    End Date

(auto_increment)       (VARCHAR)      DATE

Upvotes: 0

Views: 458

Answers (1)

Alex W
Alex W

Reputation: 38193

You would need a PHP script which looks up the date you need according to certain query parameters you specify, if necessary, in the URL and then the PHP would use echo to output that date from MySQL.

Finally, JavaScript/jQuery would need to do an AJAX request to the PHP script, where it will fetch the date as a string in its response, and then you can use it in your JavaScript code:

$.ajax({
            type:'GET',
            url: 'your_mysql_date_fetcher.php',
            data: "product_id=5&ProductName=example_name",
            success:function(data){
                var austDay = new Date(data);
                $('#defaultCountdown').countdown({until: austDay});
                $('#year').text(austDay.getFullYear());
            }
        });

Upvotes: 1

Related Questions