简体   繁体   English

如何在将PHP表单提交到同一页面之前清空页面?

[英]How To Blank The Page Before Submitting PHP Form To Same Page?

I'm trying to create an auto-response script for work which will display the form fields as required, such as Name , Username , and Password . 我正在尝试为工作创建一个自动响应脚本,该脚本将根据需要显示表单字段,例如NameUsernamePassword Once I select Submit, I want the script to remove the form, and display the output with the corresponding response in its stead without having to post it to another page. 选择“提交”后,我希望脚本删除表单,并显示输出以及相应的响应,而不必将其发布到另一页。 Ultimately having a form.php and a result.php for each individual scenario that I want will be too cluttered, hence why I'm trying to accomplish this. 最终,对于我想要的每个单独的方案,都有一个form.php和一个result.php将会太混乱,因此为什么我要尝试做到这一点。 Thus far for the form, I have: 到目前为止,对于表单,我有:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <table width="100%" cellpadding="10" cellspacing="0">
        <tr>
            <td style="width:33%">

            </td>
            <td style="width:33%">&nbsp;</td>
            <td style="width:33%">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="3" style="width:99%">
                <label>Name</label><span class="req">*</span>
                <span id="reqName" style="color:Red;display:none;">&nbsp;</span>
                <br />
                <input type="text" name="name">
            </td>
        </tr>
        <tr>
            <td colspan="3" style="width:99%">
                <label>Username</label><span class="req">*</span>
                <span id="reqProblem" style="color:Red;display:none;">&nbsp;</span>
                <br />
                <input type="text" name="username">
            </td>
        </tr>
        <tr>
            <td colspan="3" style="width:99%">
                <label for="txtProblem">Password</label><span class="req">*</span>
                <span id="reqProblem" style="color:Red;display:none;">&nbsp;</span>
                <br />
                <input type="text" name="password">
            </td>
        </tr>
        <tr>
            <td colspan="3" style="width:99%">
            <br />
            <input type="submit" name="submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

For the PHP I have: 对于PHP,我有:

<?php
if(isset($_POST['submit'])) {
    echo "<h4>Response</h4>";
    $username = $_POST['username'];
    $password = $_POST['password'];
    $name = $_POST['name'];

    echo "<p>Hello, $name <br />


    Your Login Information is as follows:
    Username: $username
    Password: $password.
</p>";
}
?>

So to sum up, I want to completely remove everything before the PHP isset and replace it with the corresponding result. 所以总结起来,我想PHP之前彻底清除一切isset ,并与相应的结果替换它。

In the present form what happens is that your HTML is sent off to the browser (or at least some invisible buffer on its way to the browser) and is out of your control once you've gotten to the part of your script that controls form processing. 在当前形式中,发生的事情是您的HTML发送到浏览器(或在到达浏览器的途中至少有一些不可见的缓冲区),并且一旦进入控制表单的脚本部分,便失去了控制。处理。

So you've got two choices. 因此,您有两种选择。 One is to leave it generally in the state you have it but use output buffering with ob_start() and related functions - which I really don't recommend in this case - or just re-arrange the order of your code. 一种是通常将其保留在您拥有的状态中,但是将输出缓冲与ob_start()和相关函数一起使用-在这种情况下,我真的不建议这样做-或只是重新排列代码的顺序。

In general the most common design pattern for these one-page self-processing forms is to have your processing logic at the very top, and the output of form or success/failure message be conditional on the results. 通常,这些单页自我处理表单最常见的设计模式是将处理逻辑放在最顶层,并且表单或成功/失败消息的输出取决于结果。

To implement this take your PHP code and move it to the top of the file. 为此,请使用您的PHP代码并将其移至文件顶部。 At the end of your present POST code, if the post was successful then you output your message but nothing else. 在当前POST代码的末尾,如果发布成功,那么您将输出消息,但没有其他输出。 If it isn't successful, or if there has not yet been any post, THEN you output the form html. 如果不成功,或者还没有任何帖子,那么您将输出html格式。 Something along these lines: 遵循以下原则:

<?php
if($no_post_or_failure)
{
// output html
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
...
<?php
} // aaaand we're done!
?>

And that's about it. 就是这样。 I hope that made sense :) 我希望这是有道理的 :)

Just change your PHP: 只需更改您的PHP:

<?php
if(isset($_POST['submit'])) {
  echo "<h4>Response</h4>";
  $username = $_POST['username'];
  $password = $_POST['password'];
  $name = $_POST['name'];

  echo "<p>Hello, $name <br />
  Your Login Information is as follows:
  Username: $username
  Password: $password.
  <a href="yourPHPfileName.php">Back</a>
</p>";
}else{
?>
//YOUR HTML FORM GOES HERE
<?php 
}

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

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