0

以下是我的 index.php 文件

<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>

下面是我的 a.php 文件

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

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

上面代码的问题是 php 文件中的 else 部分一直在运行。这意味着我一个接一个地得到 3 个随机数,但我想一个接一个地获取数组的所有三个记录。我认为isset($_GET["count"])是行不通的。但为什么,我不知道。请在这方面提供帮助。

4

1 回答 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.

于 2013-10-18T09:17:25.737 回答