0

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.

4
  • 1
    first check count is set or not then try using $_REQUEST["count"] Commented Oct 18, 2013 at 8:46
  • {count: count} <- could it be a namespace clash here? Commented Oct 18, 2013 at 8:47
  • 1
    FYI it would be better logic to make a single AJAX request to retrieve all your data, and then loop over it in the interval, instead of making x requests to your server. Commented Oct 18, 2013 at 8:53
  • @RoryMcCrossan - Thanks for your suggestion. I will redesign it. Commented Oct 18, 2013 at 8:54

1 Answer 1

2

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.