Trying to call PHP page via ajax

Hi folks

As you will probably work out, I am only just starting with javascript. (PHP I’m use to).

I’m trying to get the following bit of code to work. Basically, I want to display a page of retrieved data for a period of time then display the next group. As there are only 5 groups, I want it reset back to group 1 after it gets to 5.

Any help would be appreciated.

<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$(document).ready(function(){
    var group = 1;
    var callAjax = function(){
        $.ajax({
            method: 'get',
            url: 'get_results.php?gp=' + group,
            success: function(data){
                $("#latestresults").html(data);
            }
            if (group == 5) {
                group = 1;
            } else {
                group += 1;
            };
        });
    }
    setInterval(callAjax,3000);
});
</script>
<meta equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <?php echo "<div id='latestresults'>Loading Results....</div>"; ?>
</body>
</html>

Thanks in advance.

that code should give you a syntax error (expected ",").

anyways, you want to move that if() block into the success function.

slightly improved version:

$.get('get_results.php', { gp: group }, function(data) {
    $("#latestresults").html(data);
    group = (group % 5) + 1;
});

That was fast!

And it works as I want. Brilliant mate!

Thanks heaps! (now just gotta play with style sheets)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.