0

我有一个指向检查$_POST['submit']isset 的 php 控制器的链接。如果不是,它会显示一个表格。提交后,控制器(现在是$_POST['submit']isset)会进行一些计算,包括 unset( $_POST) 并以改变的状态重新显示表单。

这是我的问题。在表单以更改状态显示后,是否有办法在按下浏览器刷新按钮时以未更改的形式显示表单?

<?php /* Template Name: update_itn */ ?>
<?php

require ("../../../../wp-load.php");
require ("../include/constants.php");

$displayd3 = 'display:none';


if(isset($_POST['submit'])){       
   $displayd3 = 'display:block';       
   unset($_POST);
}    

?>

<!DOCTYPE html>
<html>
   <head>
      <title>Update EMAILS</title>
      <style>
         textarea{
              vertical-align:middle;
              width:100%;
              border:solid black 1px;
          }
          #d1{
              text-align:center;
          }
          #d2{
              padding:5%;
          }
          form{
              text-align:center;
              width:40%;
              position:relative;
              margin:0 auto;

          }


          span{
              display:inline-block;
              position:absolute;
              left:-40%;
              bottom:55%
          }
          button{
              margin-top:10%;
              font-size:1.75vw;
          }
          #d3{
              color:purple;
              text-align:center;
          }
      </style>
   </head>
   <body>
     <div id='d1'>
        <h1>SustainableWestonMA.org</h1>
        <h2>Add Emails to Database</h2>
     </div>

     <div id='d2'>In the textbox below type or copy and paste email addresses you would like to add to the database. 
          Be sure to seperate emails with a comma. Emails are not verified so be sure they are correct. 
          Duplicate addresses will not be added.</div> 

     <div>

       <form action='update_addEmails.php' method='POST' >

          <span>Add Emails:</span><textarea  rows='35' name='emails'  id='emails' ></textarea>  <br>
      <button type='submit' name='submit' id='submit'>Update</button>
   </form>
 </div>
 <div id='d3' style=<?php echo $displayd3;?>><h1>Emails have been added to the database</h1></div>

 </body>
</html>
4

1 回答 1

1

使用PRG 模式是要走的路。

<?php

require ("../../../../wp-load.php");
require ("../include/constants.php");

// You need to start a session if it hasn't already started.

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {       
    // do your operations here

    $_SESSION['flash'] = 'Emails have been added to the database'; // or an error message based on the result of your operation

    header('HTTP/1.1 303 See Other');
    header('Location: update_addEmails.php');
    die();
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Update EMAILS</title>
    <style>
        textarea {
            vertical-align: middle;
            width: 100%;
            border: solid black 1px;
        }
        #d1 {
            text-align: center;
        }
        #d2 {
            padding: 5%;
        }
        form {
            text-align: center;
            width: 40%;
            position: relative;
            margin: 0 auto;
        }
        span {
            display: inline-block;
            position: absolute;
            left: -40%;
            bottom: 55%
        }
        button {
            margin-top: 10%;
            font-size: 1.75vw;
        }
        #d3 {
            color: purple;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="d1">
        <h1>SustainableWestonMA.org</h1>
        <h2>Add Emails to Database</h2>
    </div>

    <div id="d2">In the textbox below type or copy and paste email addresses you would like to add to the database. Be sure to seperate emails with a comma. Emails are not verified so be sure they are correct. Duplicate addresses will not be added.</div>

    <div>
        <form action="update_addEmails.php" method="POST">
            <span>Add Emails:</span>
            <textarea rows="35" name="emails" id="emails"></textarea>
            <br>
            <button type="submit" name="submit" id="submit">Update</button>
        </form>
    </div>
    <?php if (isset($_SESSION['flash'])): ?>
        <div id="d3">
            <h1><?= $_SESSION['flash']; ?></h1>
        </div>
    <?php endif; ?>
</body>
</html>

<?php
if (isset($_SESSION['flash'])) {
    unset($_SESSION['flash']);
}
?>
于 2019-12-14T19:20:19.457 回答