user1556433
user1556433

Reputation:

How to display array items at regular intervals using jquery and PHP?

Following is my index.php file

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var count = 0;

$(document).ready(function(){
var auto_refresh = setInterval(function (){
    $('#mydiv').load('a.php', {count: count}, function () {
        count = count + 1;

        //after three attempts it won't call php file.
        if (count > 2) {
            clearInterval(auto_refresh);
        }
    }).fadeIn("slow");
    }, 1000); 
});
</script>
</head>
<body>
<div id="mydiv"> </div>
</body>
</html>

Below is my a.php file

<?php
$questions=array(
                 "Array Item 0",
                 "Array Item 1",
                 "Array Item 2");

if (isset($_GET["count"])) 
{
    echo $questions[intval($_GET["count"])];
}
else
{
    echo rand();
}
?>

Problem with above code is that else part in php file is running evertime. It means I am getting 3 random numbers one by one but I want to get all the three records of array one by one. I think isset($_GET["count"]) is not working. But why, I don't know. Please help in this.

Upvotes: 0

Views: 270

Answers (1)

jacouh
jacouh

Reputation: 8741

This a.php worked for me:

<?php
$questions=array(
             "Array Item 0",
             "Array Item 1",
             "Array Item 2");

if (isset($_POST["count"])) 
{
    echo $questions[intval($_POST["count"])];
}
else
{
    echo rand();
}
?>

Your data are "POSTed" ?

ADDED:

I have found this in jQuery page:

Request Method

The POST method is used if data is provided as an object; otherwise, GET is assumed.

Upvotes: 2

Related Questions