简体   繁体   English

php javascript刷新页面

[英]php javascript refresh page

I have (working): first page - user inputs the string and presses submit button. 我有(正在工作):第一页-用户输入字符串,然后按提交按钮。 This parameter, via session is sent to the other page which runs some kind of script command and shows the result (output). 该参数通过会话发送到另一个页面,该页面运行某种脚本命令并显示结果(输出)。 Here' the code to make it more clear: 这是使代码更清楚的代码:

First page: 第一页:

<?php
    session_start();
    echo "<input type=\"text\" id=\"userInput\">";
    echo "<br><button onclick=\"submit()\">Submit</button>";
    $_SESSION[''] = $input;
?>

<script type="text/javascript">
    var submit = function() {
        input = document.getElementById("userInput").value;
        window.open ('secondPage.php?userInput=' + input,'_self',false);}
</script>

Second page: 第二页:

<?php
    session_start();
    $input = $_GET['userInput'];
    $command = "./myScript.py $input";
    system($command, $retval);
?>

Now I'd like to have both in one single page, ie when the page opens, the input field and button would be on top and under it would be the output of the command of the second page, refreshed each time user "submits" another input (first time the userInput would be empty). 现在,我想将两者都放在一个页面中,即,当页面打开时,输入字段和按钮将位于顶部,而其下方将是第二页命令的输出,每次用户“提交”时都会刷新另一个输入(第一次userInput将为空)。 Hope it's clear, more or less. 希望或多或少都清楚。 Any help? 有什么帮助吗?

I guess this is what you need : 我想这就是您需要的:

<?php
    session_start();

    // We check if there's POST data 'userInput'
    if (isset($_POST['userInput'])) {
        // 'userIput' exists, we make our process and return what we need.
        echo 'User inputed '.$_POST['userInput']."<br>";
    } else {
        // 'userIput' does not exists, displaying page content.
        ?>
        <input id="userInput" type="text" /><button id="btn">Send</button>
        <div id="result">
            <!-- Displaying AJAX results here -->
        </div>
        <script type="text/javascript">

            // Send an XMLHttpRequest
            function sendRequest(url, postData, callback) {
                var req = new XMLHttpRequest();
                if (!req)
                    return;
                var method = (postData) ? "POST" : "GET";
                req.open(method,url,true);
                if (postData)
                    req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
                req.onreadystatechange = function () {
                    if (req.readyState != 4)
                        return;
                    if (req.status != 200 && req.status != 304) {
                        return;
                    }
                    callback(req);
                }
                if (req.readyState == 4)
                    return;
                req.send(postData);
            }

            // We bind the 'click' event on the button
            document.getElementById("btn").addEventListener('click', function(e) {
                var input = document.getElementById("userInput").value;
                // Use AJAX
                sendRequest("#", 'userInput='+input, function(data) {
                    // Data returned, inserting in our 'result' div.
                    var resDiv = document.getElementById("result");
                    resDiv.innerHTML = resDiv.innerHTML + data.responseText;
                });
            });
        </script>

        <?php
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM