PHP ob_end_flush与JQuery

时间:2015-11-09 19:32:12

标签: php jquery

我遇到了使用jquery的ob_implicit_flush问题。我正在使用jqueryUI小部件选项卡来显示2个选项卡式表单。在我的过程中,我正在打印一个蚂蚁任务的实时输出。其代码如下

if (isset($cleaned)){
    echo '<div id="ant">';
    $json_arg = escapeshellarg($cleaned);
    while (@ob_end_flush());
    ob_implicit_flush(true);
    $proc = popen("sudo /var/www/html/scripts/set_vars.sh $json_arg 2>&1", 'r');
    echo '<pre>';
    while (!feof($proc)){
        echo fread($proc, 4096);
        @ flush();
    }
    echo '</pre>';
    echo '</div>';
    echo '<script>';
    echo 'setTimeout( function ( ) { alert( "Event Added!" ); }, 2000 );';
    echo 'clearform();';
    echo '</script>';
}

它工作正常但是当调用ob_end_flush时,所有表单样式也都被刷新(由jquery创建)。任务完成后,样式返回。当这个任务正在运行时,有没有人知道如何避免我猜jquery?

由于

编辑:

这是完整的代码

        <html>
            <head>
                <link rel="stylesheet" href="css/jquery-ui.css">
                <link rel="stylesheet" type="text/css" href="css/style.css"/>
                <script src="scripts/jquery-ui.min.js"></script>
                <script src="scripts/jquery-ui.js"></script>
                <script language="javascript">
                    function clearform() {
                        document.getElementById("json").value="";
                    }
                </script>
                <script>
                    $(function() {
                        $( "#tabs" ).tabs();
                    });
                </script>
                <title>Add Manual Event Data</title>
                <?php
                    if (empty($_POST["comment"])) {
                        $comment = "";
                    }
                    else {
                        $cleaned = test_input($_POST["comment"]);
                        $comment = ($_POST["comment"]);
                    }

                    function test_input($data) {
                        $data = trim($data);
                        $data = addslashes($data);
                        return $data;
                    }
                ?>
            </head>
            <body>
                <div id="tabs" class="body-check">
                    <ul>
                        <li><a href="#tabs-1">Event By String</a></li>
                        <li><a href="#tabs-2">Add Event Form</a></li>
                    </ul>
                    <div id="tabs-1">
                        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                            <center><h1>Add Event Data:</h1></center>
                            <p>
                                <textarea id="json" name="comment"><?php echo $comment;?></textarea>
                            </p>
                            <input type="submit" name="submit" value="Submit">
                        </form>
                        <?php
                            if (isset($cleaned)){
                                echo '<div id="ant">';
                                $json_arg = escapeshellarg($cleaned);
                                while (@ob_end_flush());
                                ob_implicit_flush(true);
                                $proc = popen("sudo /var/www/html/scripts/set_vars.sh $json_arg 2>&1", 'r');
                                echo '<pre>';
                                while (!feof($proc)){
                                    echo fread($proc, 4096);
                                    @ flush();
                                }
                                echo '</pre>';
                                echo '</div>';
                                echo '<script>';
                                echo 'setTimeout( function ( ) { alert( "Event Added!" ); }, 2000 );';
                                echo 'clearform();';
                                echo '</script>';
                            }
                        ?>
                    </div>
                    <div id="tabs-2">
                        <h1>Hello!</h2>
                    </div>
                </div>
            </body>
        </html>

由于

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法。我创建了一个名为ant.php的文件,然后我在iFrame中调用它。这样ob_flush就不会影响父脚本

<强>的index.php

<html>
    <head>
        <link rel="stylesheet" href="css/jquery-ui.css">
        <link rel="stylesheet" type="text/css" href="css/style.css"/>
        <script src="scripts/jquery-ui.min.js"></script>
        <script src="scripts/jquery-ui.js"></script>
        <script language="javascript">
            function clearform() {
                document.getElementById("json").value="";
            }
        </script>
        <script>
            $(function() {
                $( "#tabs" ).tabs();
            });
        </script>
        <title>Add Manual Event Data</title>
        <?php
            if (empty($_POST["comment"])) {
                $comment = "";
            }
            else {
                $cleaned = test_input($_POST["comment"]);
                $comment = ($_POST["comment"]);
            }

            function test_input($data) {
                $data = trim($data);
                $data = addslashes($data);
                return $data;
            }
        ?>
    </head>
    <body>
        <div id="tabs" class="body-check">
            <ul>
                <li><a href="#tabs-1">Event By String</a></li>
                <li><a href="#tabs-2">Add Event Form</a></li>
            </ul>
            <div id="tabs-1">
                <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                    <center><h1>Add Event Data:</h1></center>
                    <p>
                        <textarea id="json" name="comment"><?php echo $comment;?></textarea>
                    </p>
                    <input type="submit" name="submit" value="Submit">
                </form>

                <?php
                    if (isset($cleaned)){
                        session_start();
                        $_SESSION['json_string'] = $cleaned;
                        echo '<div id="ant"><iframe src="ant.php"></iframe></div>';
                        echo '<script>clearform();</script>';
                    }
                ?>
            </div>
            <div id="tabs-2">
                <h1>Hello!</h2>
            </div>
        </div>
    </body>
</html>

<强> ant.php

<?php
    session_start();
    $json_cleaned = $_SESSION['json_string'];
    $json_arg = escapeshellarg($json_cleaned);
    $proc = popen("sudo /var/www/html/scripts/set_vars.sh $json_arg 2>&1", 'r');
    echo '<pre>';
    while (!feof($proc)){
        echo fread($proc, 256);
        @flush();
        @ob_flush();
        echo "<script>window.scrollTo(0,99999);</script>";
        usleep(200);
    }
    pclose($proc);
    echo '</pre>';
    echo '<script>';
    echo 'window.scrollTo(0,99999);';
    echo 'setTimeout( function ( ) { alert( "Event Added!" ); }, 2000 );';
    echo '</script>';
?>

感谢所有帮助,以为我分享了我的解决方案