php - 我可以让实时 PHP 数据显示在 jquery 对话框中吗?

标签 php jquery html ajax

我正在尝试将两个想法结合起来,但我不确定它们是否相互兼容。

想法1: 让 php 脚本运行命令(例如:ping)并在 Web 浏览器中提供命令的实时结果。

想法2: 出现一个 jQuery 对话框,打开时运行 php 脚本并在对话框中提供实时结果。

想法 1 相当容易实现 (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!!

想法 2 给我带来了麻烦。 我可以打开对话框,但直到 php 完成工作后才会出现数据。这可能是 ajax 的本质,所以我可能在正确的方法上偏离了目标。

这是我的index.html 中的javascript:

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

有人对这是否可能有任何想法吗? 如果是这样,您对如何实现这一目标有什么建议吗?

最佳答案

使用jQuery.get()请求您的 PHP 页面,getScript用于加载 javascript 文件。

$.get('yourpage.php', {}, function(data, status, xhr) {
    // all done, get your content from the data variable.
});

如果您在 get 调用的正文中显示弹出窗口,而不是从对话框中调用 get,则对话框将在拥有所有数据后显示。

编辑:AJAX 似乎在显示任何信息之前等待readyState 4。 PHP刷新似乎发送了3的readyState。You will need to listen for that and fill in the responseText .

有一些潜在的错误可能需要 disabling compressionsetting the content type to application/octet-stream .

关于php - 我可以让实时 PHP 数据显示在 jquery 对话框中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931775/

相关文章:

php - 将用户重定向到我的分页上的最后一页

php - MySQL DB 字段的多个数据

jquery - 如何将 Roundabout 制作的图片库与 Fancybox 集成?

javascript - 将输入文件值获取到文本框中

javascript - 重复显示数据

javascript - Jquery 拖放 div

php - 需要一些关于 PHP/mysql 错误处理的建议

php - FreeBSD 中的简单反 DDoS 保护

javascript - jEditable 取消后如何格式化字段值

html - 如何在不丢失比例的情况下使背景图像具有响应性