Php JQuery更新状态

Php JQuery更新状态,php,jquery,ajax,Php,Jquery,Ajax,我正在编写一些jquery,每2秒调用一个ajax脚本,以获得结果并更新页面。我主要是一个后端程序员,在这方面需要一些帮助 这是我现在拥有的代码: <script language="javascript"> function downloadProgress(id) { $("#" + id + "").load("index.php?_controller=download&_action=getDownloadProgressAjax",

我正在编写一些jquery,每2秒调用一个ajax脚本,以获得结果并更新页面。我主要是一个后端程序员,在这方面需要一些帮助

这是我现在拥有的代码:

<script language="javascript">
function downloadProgress(id) {
     $("#" + id + "").load("index.php?_controller=download&_action=getDownloadProgressAjax",
            {
                downloadId: id
            }
        );
    setTimeout(downloadProgress(id), 2000);
}
</script>
<?php
    foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>">
    <script language="javascript">
        downloadProgress(<?php echo $dl["download_id"]; ?>);
    </script>
</div>
<?php        
    }
?>
这是行不通的。我做错了什么?或者你会建议另一种方法吗


谢谢

因为您使用的是jquery,所以当页面准备就绪时,您可以使用$.ajax函数

$(function () {
  function function downloadProgress(id) {
     $.ajax({
         url: "index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id
       })

     setTimeout(function () {
        if (downloadnotcomplete){ // this way your script stops at some pont.
           downloadProgress(id); 
        }
     },2000);
  }
});

您将把downloadProgressid函数附加到下载按钮或其他任何东西上,以便第一次触发该函数。

由于您使用的是jquery,因此在页面准备就绪时可以使用$.ajax函数

$(function () {
  function function downloadProgress(id) {
     $.ajax({
         url: "index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id
       })

     setTimeout(function () {
        if (downloadnotcomplete){ // this way your script stops at some pont.
           downloadProgress(id); 
        }
     },2000);
  }
});

您将把downloadProgressid函数附加到下载按钮或其他任何东西上,以便在第一次触发该函数。

我认为您给PHP脚本提供了作为GET发送的查询字符串变量和可能作为POST发送的数据,这是在混淆PHP脚本。试试这个:


$+id.loadindex.php?\u controller=download&\u action=getDownloadProgressAjax&downloaddid=+id}

我认为您给php脚本提供了作为GET发送的查询字符串变量和可能作为POST发送的数据,这是在混淆php脚本。试试这个:


$+id.loadindex.php?\u controller=download&\u action=getDownloadProgressAjax&downloadId=+id}

您遇到的问题是必须提供一个无参数函数,而不是对setTimeout的函数调用。另外,我会做一些不同的事情,使用setInterval而不是setTimeout,因为它在代码中更好地传达了您的意图。我会这样做:

<script language="javascript">
    $(function() {
        setInterval(downloadHandler, 2000);
    });
    function downloadHandler() {
        // I'm not sure where the id is coming from  you will probably need to put a
        // class on your div's so that you can select them.
        $(".MyDivClass").each(function() {
            var id = $(this).attr("id");
            downloadProgress(id);
        });
    }
    function downloadProgress(id) {
        $("#" + id + "").load(
            "index.php?_controller=download&_action=getDownloadProgressAjax",
            { downloadId: id }
        );
</script>
然后在你的div上:

<?php
    foreach ($downloads as $dl) {
?>
    <div id="<?php echo $dl["download_id"]; ?>" class="MyDivClass"/>
<?php        
    }
?>

希望这有帮助。

您遇到的问题是必须提供无参数函数,而不是对setTimeout的函数调用。另外,我会做一些不同的事情,使用setInterval而不是setTimeout,因为它在代码中更好地传达了您的意图。我会这样做:

<script language="javascript">
    $(function() {
        setInterval(downloadHandler, 2000);
    });
    function downloadHandler() {
        // I'm not sure where the id is coming from  you will probably need to put a
        // class on your div's so that you can select them.
        $(".MyDivClass").each(function() {
            var id = $(this).attr("id");
            downloadProgress(id);
        });
    }
    function downloadProgress(id) {
        $("#" + id + "").load(
            "index.php?_controller=download&_action=getDownloadProgressAjax",
            { downloadId: id }
        );
</script>
然后在你的div上:

<?php
    foreach ($downloads as $dl) {
?>
    <div id="<?php echo $dl["download_id"]; ?>" class="MyDivClass"/>
<?php        
    }
?>

希望这能有所帮助。

{downloaddid:id}是一个作为POST数据发送到服务器的对象。你是对的,他需要使用get或post,但不能同时使用两者。谢谢,我已经更新了我的帖子-我找不到jQuery API中提到的[data],因为我读的是event.load,而不是ajax.load-P{downloadId:id}是一个作为POST数据发送到服务器的对象。你是对的,他需要使用get或post,但不能同时使用两者。谢谢,我已经更新了我的帖子-我找不到jQuery API中提到的[data],因为我读的是event.load,而不是ajax.load-P