使用jQuery /阿贾克斯用PHP来显示当前进度 [英] Using jQuery/Ajax with PHP to display current progress

查看:107
本文介绍了使用jQuery /阿贾克斯用PHP来显示当前进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短而亲切: 寻找一种方式来调用PHP文件并显示使用jQuery和/或阿贾克斯的进展。 PHP文件upgrade.php?步= 1被调用,然后返回的输出追加到#upgradestatus。在此之后完成upgrade.php?步骤= 2被调用和输出追加,直到指定数目的步骤就完成了。

Short and sweet: Looking for a way to call PHP file and display the progress using jQuery and/or Ajax. PHP file upgrade.php?step=1 is called and then the output returned is appended to #upgradestatus. After that is completed upgrade.php?step=2 is called and that output is appended until the specified number of steps is completed.

说明: 我试图创建一个插件运行在PHP软件。这个插件将调用通过PHP文件中的每个步骤,每个升级文件,等等。我希望能够无需重新加载它显示用户在页面上显示的进展做了什么以及其中的过程是

Explanation: I'm attempting to create a plugin for software that runs in PHP. This plugin will call each step through the PHP file to upgrade each file, etc. I want to be able to display the progress on the page without reloading it to show the user what has been done and where the process is at.

使用我的PHP的思维过程,我想创建一个类似于这样:

Using my PHP thought process I wanted to create something similar to this:

for (var s=0; s<2; s++){
    $('#upgraderesult').load('upgrade.php', 'step=' + s, function() {
        $('#upgradestatus').append('Upgrade Step ' + s + ' Completed');
    });
}

我试图用阿贾克斯(),以及与这两种方法我很快明白了是异步这将是一个有点棘手搞清楚。

I attempted to use .ajax() as well and with both of these methods I soon learned that being Async this is going to be a little more tricky to figure out.

我这背后的想法是创建一个for循环将循环的步量存在。对于循环然后将生成所需的jQuery的/阿贾克斯code,将追加到现有的div #upgradestatus了状态消息。我希望它附加在#upgraderesult,而是保持简单我刚刚加入的升级步骤×完全装的结果。

My thought behind this was to create a FOR loop that would loop for the amount of steps there are. That for loop would then generate the necessary jQuery/Ajax code that will append to the existing div #upgradestatus with the status message. I wanted it to append the result loaded in #upgraderesult but to keep it simple i just added the "Upgrade Step X Complete".

所以现在我卡住,想我可能只需要使用PHP来生成正确的jQuery /阿贾克斯code,但我想检查,看看其他人对如何能够做到这一点的任何意见或建议以不同的方式,甚至如果我想这完全是错误的。

So now i'm stuck and thinking I may just need to use PHP to generate the correct jQuery/Ajax code but I wanted to check and see if anybody else has any opinions or suggestions on how this could be done a different way or even if i'm thinking about this complete wrong.

我要的是能够调用基于关闭的过程中一步的PHP文件,然后显示进度,在浏览器的用户......这已被证明比我想象的困难得多。

All I want is to be able to call the PHP file based off the step in the process and then display the progress to the user in the browser...which has turned out to be a lot more difficult than i thought.

感谢您的任何输入或建议/建议,我非常AP preciate了!

Thanks for any input or recommendations/suggestions, I greatly appreciate it!

推荐答案

每个人都感谢您的输入,从一些人在Freenode的#jQuery我们能够想出这个它完美的作品的帮助:

Everyone thank you for your input, with help from some of the guys in the Freenode #jQuery we were able to come up with this which works perfectly:

var numSteps = 2;
function loadStepAjax(s) {
    $.ajax('upgrade.php', {
        type: 'GET',
        dataType: 'html',
        data: {
           step: s
        },
        success: function(data) {
            $('#upgradestatus').append(data);
            if (s < numSteps)
                loadStepAjax(s+1);
            }
     });
}

您然后调用该函数:

loadStepAjax(1);

代替使用FOR循环的

我们创造了一个功能loadStepAjax,然后用于的IF语句就回调来确定是否再次调用该函数。

Instead of using a FOR loop we created a function loadStepAjax and then used an IF statement on the callback to determine whether to call the function again.

它总是简单的东西是不是!感谢大家的投入虽然,这是极大的AP preciated。

It's always something simple isn't it! Thanks for everyone's input though, it is greatly appreciated.

这篇关于使用jQuery /阿贾克斯用PHP来显示当前进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆