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
3
  • 1
    The overly simple answer is: AJAX. api.jquery.com/jQuery.ajax Commented Mar 19, 2014 at 16:16
  • By hardcoded, do you mean austDay.getFullYear() + 1, 1 - 1, 26 ? Commented Mar 19, 2014 at 16:18
  • @AlexW Yes, I meant that. Commented Mar 19, 2014 at 16:32

1 Answer 1

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());
            }
        });
Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
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 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.
I've made a new question here. stackoverflow.com/questions/22515988/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.