0

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
NetStack
  • 206
  • 2
  • 3
  • 11

1 Answers1

1

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());
            }
        });
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • Hey mate, I'm making an ecommerce deal website. On the index page, there would be multiple deals displayed, with each deal having it's own end date. What would be the best way to use this above code for all the multiple products ? And getting the end date for all those products. – NetStack Mar 19 '14 at 18:33
  • @NetStack You see the `data:` section where I pass parameters? You can just change the `ProductName` etc. there. You will have to build the PHP script that runs your MySQL queries first, so you will have a better idea of what the name of the query strings will be. – Alex W Mar 19 '14 at 18:47
  • Is it possible to run this above script in a php while loop, and then somehow passing productid so I can fetch the end date within the ajax call. – NetStack Mar 19 '14 at 18:56
  • @NetStack It is possible to pass all of the product IDs to the PHP script in a single AJAX call and receive all of the end dates back. If you want to know how to do that, you should create a new question. – Alex W Mar 19 '14 at 19:07
  • I've made a new question here. http://stackoverflow.com/questions/22515988/passing-php-values-in-an-ajax-call – NetStack Mar 19 '14 at 19:16