user2090111
user2090111

Reputation: 3

Auto updating Image using mysql php and i would assume javascript

Alright, So I've searched google for what i'm trying to do and can't seem to find what i am exactly looking for.

What I would like to do is have a image that is updated every second, this image is used for a message icon, similar to that of Facebook. No page refresh, Need the element updated using Jquery(preferred). I know very little on the java-script side of this, however the PHP and MySQL side I can do as long as i know what to tell the java-script to do.

I guess the real Question would be: How can i make the image update based upon, any change in the MySQL database? Any help on this would be GREATLY appreciated.

Upvotes: 0

Views: 129

Answers (2)

Alfred
Alfred

Reputation: 21386

You may first set an interval using jQuery. Refer this question;

JavaScript - jQuery interval

Then you may do ajax requests on that intervals to check for new entries in database. In MySQL, make a flag entry to identify whether the entry is new or old (say set 0 for old and 1 for new). On each ajax request, if there is any entry with status 1, then the PHP may return that number of entries having 1 (ie, may be greater than 1) as the ajax result. Then the status should be set to 0. In javascript ajax, you may check whether the return value is 0 or else. If 0, then you need not do anything. If 1, then you may change some element's css property (like changing background color), so as to make it a notification. When user click that element, the css may be brought back to default.

Upvotes: 0

DeBaum
DeBaum

Reputation: 130

Try something like:

window.setInterval(function() {
    $.get("url", { parameters: egTime}, function(response) {
        if($("#myImage").attr("src") != response)
            $("#myImage").attr("src", response);
    })
}, 1000);

the server has to return the new src of the image as a string ;-)

Upvotes: 1

Related Questions