How to reset form from php controller

120 views Asked by At

I have a link to a php controller that checks if $_POST['submit'] isset. If not it displays a form. When submitted the controller (now that $_POST['submit'] isset) does some calculations including unset($_POST) and redisplays the form in an altered state.

Here's my problem. After the form is displayed in an altered state is there a way to display the form in its unaltered form when the browsers refresh button is pressed?

<?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>
1

There are 1 answers

2
N'Bayramberdiyev On

Using PRG pattern is the way to go.

<?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']);
}
?>