简体   繁体   English

用PHP刷新页面

[英]Refreshing Page With Php

I'm working on a php code where I'm echoing out form values and a file onto the page. 我正在处理一个php代码,在其中回显表单值和文件到页面上。 I have that working fine but I want to implement a start over button when pressed will display the page again without the echoes. 我的工作正常,但是我想实现一个重新开始按钮,当按下该按钮时,将再次显示该页面而没有回显。 Any suggestions or better ways to do this? 有什么建议或更好的方法吗?

<html>
 <head>

 </head>
 <body>
<div class="page">
    <h1>File Loader</h1>
    <form method="post">
        <label>Name:</label><input type="text" name="name"></br>

        <label>Date:</label><input type="text" name="date"></br>

        <input type="submit" name="submit" value="Submit">
        <input type="button" name="startOver" value="Start Over" onclick="phpfile.php"></br>
    </form>
</div>

<?php
if ($_POST['submit']) {

    $name = $_POST['name'];
    $date = $_POST['date'];

    echo "Name: $name</br></br>";
    echo "Date: $date</br>";

    $file_handle = fopen("file.csv", "r");


        while (!feof($file_handle) ) {

            $fields_of_text = fgetcsv($file_handle, 2024);

            $f = $fields_of_text;
            $format = "%s %s %s %s %s %s %s %s %s %s %s";
            echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);

            echo "<br>";

            echo "<br>";
            echo "<br>";
        }

        fclose($file_handle);


 } else {
    header("Location: phpfile.php");
}
?>
</div>
</body>
</html>

You can easily use query string/post params. 您可以轻松使用查询字符串/后置参数。

You can use $_GET like below or you can use a hidden input and change that to 1 or 2 with the onclick and javascript if you need to use post. 您可以像下面那样使用$ _GET,也可以使用隐藏的输入,如果需要使用post,可以使用onclick和javascript将其更改为1或2。

 </head>
 <body>
<div class="page">
    <h1>File Loader</h1>
    <form method="post">
        <label>Name:</label><input type="text" name="name"></br>

        <label>Date:</label><input type="text" name="date"></br>


        <input type="button" value="Submit" onclick="window.location.href='phpfile.php?SubmitType=1';">
        <input type="button" value="Start Over" onclick="window.location.href='phpfile.php?SubmitType=2';">

        </br>
    </form>
</div>

<?php
if ($_GET['SubmitType'] == '1') {



    $name = $_POST['name'];
    $date = $_POST['date'];

    echo "Name: $name</br></br>";
    echo "Date: $date</br>";

    $file_handle = fopen("file.csv", "r");


        while (!feof($file_handle) ) {

            $fields_of_text = fgetcsv($file_handle, 2024);

            $f = $fields_of_text;
            $format = "%s %s %s %s %s %s %s %s %s %s %s";
            echo "<br>" . sprintf($format, $f[0], $f[1], $f[2], $f[3], $f[4], $f[5], $f[6], $f[7], $f[8], $f[9], $f[10]);

            echo "<br>";

            echo "<br>";
            echo "<br>";
        }

        fclose($file_handle);


     } 
     else {
        header("Location: phpfile.php");
        exit();
     }
?>
</div>
</body>
</html>

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

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