I am designing a page where I need it to update itself only if a numeric value in my MySQL database has changed. Unfortunately, I cannot just use meta-refresh tags or similar, because this page cannot be reloading unless that data value has changed. So the solution I have been researching is using XML HTTPREQUEST or some ajax. Problem is, javascript and ajax is too new for me. Everything else on the page will be made with PHP.

The person that views this page will not be interacting, but rather sitting back and seeing changes only if I change the database value. Sounds like I might need a timer or something just to check if the database has changed, and if it has, then refresh the page.

Any thoughts? Anyone done something like this before?

    My initial thought is that you can only get PHP to execute when the client sends an HTTP request; there's no way that a PHP script (or anything server side) can send information back to a client without a request from the client.

    Try asking some JavaScript experts as well.

      XML HTTPREQUEST and ajax are basically the same thing, and is one way to do this. A hidden iframe can net you the same result and be more compatible with older browsers. If you can put some limits on browsers supported, I'd go the ajax way. Of course if you're doing ajax anyway you might as well not refresh and just change the content on the page using javascript.

      Anyway, ajax good, and will get the job done. I'd suggest you read some tutorials on it. If you use something like jpspan it's pretty simple to implement.

        Hidden iframes aren't needed anymore as AJAX is around now. I can definitely help you with this; AJAX is in my title! 😉

        You're going to want to set up a JS function that queries a PHP script. The PHP script in turn queries the DB to see if anything has changed, and it returns some data. Then JS can traverse the data using its native DOM support. I'd reccomend using setInterval() to set a JavaScript function to run every n seconds; the function will create the XMLHttpRequest object, query the server, receive the data and analyze it, then deciding what action to take from there.

          Thanks for the help thus far. I would prefer to stay away from the iframe; as people using this part of the site will be using newer browser technology and I would like to learn a little something about ajax.

          LoganK,
          I got a little bit of help on another form and here is what the guy suggested:

          http://www.codingforums.com/showthread.php?t=64511

          I have set everything up, but it is not working. My biggest question is what should the hasChanged.php file return? Right now it just returns the numeric value from the database, but that does nothing.

          Thanks,
          Joe

            It should return whatever you want it to return - but bear in mind that you're processing via JavaScript. I'd have it a return a very simple, plain-text, "1" or "0" - 1 if the DB changed, 0 if it didn't. Then your JS processor can say "if the result is 1, refresh the page. If the result is 0, don't refresh the page. If the result is neither, error!".

              Sorry for my ignorance, but how would you get a returned value from a php file loaded into a JS var. For example, my php file will yeild 1 if the database changes, but then in the JS, how do I get that returned php value into a var to test it?

              Thanks again,
              Joe

                Once you have the AJAX container (XMLHttpRequest) set-up, query the PHP file, and access the returned result through the correct object. I think it's "responseText".

                  I've made some progress...

                  So in testing if the database value has changed, the only way that I can think of doing it would be to put in a timestamp into the database each time it changes and then in my hasChanged.php I can simply test if the time since the last timestamp is less than x; then return 1 or 0.

                  Does anyone have any other ideas? I sort of wanted to stay away from the timestamp, because I will be checking a database change every 2 seconds, and if for some reason a user takes longer than 2 seconds, they won't get an update.

                  I attempted to do it via a session variable, but the main page does not update the session variable in the javascript each time it queries hasChanged.php (since the page is not reloading).

                    You can do something similar - take the main content of the database, and when you first serve it to the user, save it as a file. Then, on subsequent "change" checks, you can check if the data in the DB is different from the file!

                      Well, as much I really wanted to get the XMLhttprequest method working, it was just too much of a pain. So, I opted for the iFrame option, which I got up and running in about an hours time. If anyone has a working example of doing something like I want to do with XMLhttprequest, please let me know.

                      Thanks for everyone's help!

                        Sorry you couldn't get XMLHTtpRequest to work. At least your problem is solved!!

                          Write a Reply...