0

I am trying to refresh a php script to show updated content as a database updates. I first built my php, then the code to refresh and then merge them. However, the script doesn't update. Anyone know why?

<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $(document).ready(
            function() {
                setInterval(function() {
                    if(document.getElementById('gallery') != null){
                        function showLots() {
                            if (window.XMLHttpRequest) {
                                // code for IE7+, Firefox, Chrome, Opera, Safari
                                xmlhttp = new XMLHttpRequest();
                            } else {
                                // code for IE6, IE5
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            xmlhttp.onreadystatechange = function() {
                                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                    document.getElementById("gallery").innerHTML = xmlhttp.responseText;
                                }
                            }
                        xmlhttp.open("GET","getuser.php",true);
                        xmlhttp.send();
                        } 
                    }
                }, 3000);
            });
</script>

Thanks.

7
  • If you've included jQuery, why wouldn't you use $.ajax, it's one of the best parts ? Commented Apr 21, 2015 at 5:13
  • Just learning how to do this. Please share. I'm curious. Commented Apr 21, 2015 at 5:13
  • Do you have an element with id="gallery"? Commented Apr 21, 2015 at 5:13
  • jsfiddle.net/adeneo/mrv93on7 Commented Apr 21, 2015 at 5:15
  • @NormanBreau yes, gallery is there Commented Apr 21, 2015 at 5:16

2 Answers 2

2

You did not called method showLots, First define it outside the function and than call it in setInterval

Sign up to request clarification or add additional context in comments.

Comments

0

The issue with your code is that function showLots() is inside your if (document.getElementById('gallery') != null) conditional without the function actually executing.

Below is what your corrected code could could look like by moving the function showLots() definition up. showLots() is then called inside where you originally had the definition.

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    function showLots() {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("gallery").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getuser.php",true);
        xmlhttp.send();
    } 

    $(document).ready(function () {
        setInterval(function () {
            if (document.getElementById('gallery') !== null) {
                showLots();
            }
        }, 3000);
    });
</script>

2 Comments

Thanks, still struggling. I'm getting an unexpected token at 3000.
Thanks. Got it now. Appreciate your help.

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.