我可以得到实时的PHP数据显示在一个jQuery的对话框?可以得到、对话框、实时、数据

2023-09-11 22:32:58 作者:奈何情深却缘浅°

我想结合两个想法,但我不知道他们是相互兼容的。

I'm trying to combine two ideas but I am not sure if they are compatible with one another.

理念1: 有一个PHP脚本运行一个命令。(EG:平),并在Web浏览器中提供该命令的结果直播

Idea 1: Have a php script run a command (EG: ping) and provide live results of the command in the web browser.

理念2: 有一个jQuery的对话框出现其中,开放,运行PHP脚本,并提供实时的结果对话框。

Idea 2: Have a jQuery dialog box appear which, on open, runs the php script and provides the live results in the dialog box.

1想法是相当容易实现(ping.php):

Idea 1 was fairly easy to accomplish (ping.php):

<?php
header('Content-Type: text/html; charset=utf-8');
set_time_limit(1800);
ob_implicit_flush(true);
ob_end_flush();

$exe_command = 'C:\\Windows\\System32\\ping.exe -n 10 google.com';

$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout -> we use this
    2 => array("pipe", "w")   // stderr 
);
flush(); 

$process = proc_open($exe_command, $descriptorspec, $pipes);
echo "<pre>";  

if (is_resource($process))
{
    while ($s = fgets($pipes[1])) {  
        print $s; 

        flush();  
    }  
}
echo "</pre>"; 
?>

如果我在浏览器中打开ping.php,我收到了一行行响应YAY !!

If I open ping.php in my browser I get a line by line response YAY!!

2想法是什么给我找麻烦。 我可以在对话框打开,但数据并没有出现,直到后PHP完成工作。这可能是阿贾克斯的性质,所以我可能是遥远的正确方法标记做到这一点。

Idea 2 is whats giving me trouble. I can get the dialog box to open but the data does not appear until after the php finished working. This is likely the nature of ajax so I am probably way off the mark on the right way to do this.

下面是JavaScript我在我的index.html:

Here is the javascript I have in my index.html:

<script language="Javascript">

function testGetScript() {
    $.getScript("./cgi-bin/ping.php", function(data) {
        var divResults = document.getElementById('pingMe');
        divResults.innerHTML = data;
    });
}

function initDialogs() {
    $('#testDialog').dialog({
        autoOpen: false,
        width: 800,
        title: "PINGING.....",
        modal: true,
        open: function(event, ui) {
            testGetScript();
        },
        close: function() {
            $(this).dialog("close");
            },
        buttons: [
            {text: "Done", click: function() {
                $(this).dialog("close");
            }}
        ]
    });

}

$(document).ready(function(){
    initDialogs();

    $("*[class='btn']").button().click(function() {
            $('#testDialog').dialog('open');
    });

</script>

任何人有这是否可能有什么想法? 如果是的话你有关于如何可以完成任何意见?

Anyone have any thoughts on whether or not this is possible? If so do you have any advise on how this can be accomplished?

推荐答案

作为建议我打了我的工作,围绕为回答这个问题,让其他人可以从中受益。

As suggested I am marking down my work around as an answer to this question so that others may benefit from this.

首先,我添加一个iFrame在index.html的对话框:

First I added an iFrame to the dialog box in the index.html:

<div id="pwrShellDiagWC" style="display: none;">
<iframe id="ajaxFrameWC" src="" width="100%" height="60%"; frameborder="0"></iframe>
</div>

然后在Javascript(使用jQuery)我初始化此对话框中添加code开放选项加载PHP网址到的iFrame:

Then in Javascript (using jQuery) I initialized this dialog box adding code to the "open" option to load the php url into the iFrame:

$('#pwrShellDiagWC').dialog({
autoOpen: false,
width: 800,
title: "Processing your request",
resizable: false,
modal: true,
open: function(event, ui) {
    var frameAttrib = $('#ajaxFrameWC').attr('src');
    var phpURL = './cgi-bin/test.php?value1=arg1';

    if (frameAttrib) {
         // do nothing!
    }
    else {
         $('#ajaxFrameWC').attr('src',phpURL)
    }
});

我有一些按钮添加为好,但我不相信你需要看到的是这个工作。 该test.php的使用code我把我的问题,并显示一行一行的对话框。

I had some buttons added as well but I don't believe you need to see that for this to work. The test.php uses the code I put in my question and displays line by line in the dialog box.

我的工作了,现在唯一的方面是强制的iFrame留滚动底部,但我相信一些有条件的搜索会得到我的过去小冲了进来。

The only aspect I am working out now is to force the iFrame to stay scrolled at the bottom but I am sure a few well placed searches will get my past that small hurtle.

干杯,希望这是有帮助的一些!

Cheers, Hope this is helpful to some!