Core PHP

Submission Check

Oftentimes, when we create a form, we want to be able to test whtether or not the form was submitted. This can be done by calling the function isset() on the variable $_POST['Submit'].

Inside the form, we use the variable $_SERVER['PHP_SELF'] to retrieve the filename of the script that is running. The function htmlspecialchars() is called on the filename to prevent malicious attacks.

FormSubmissionCheck.php

<?php
  // Use the "Submit" to check whether original form was submitted.
  if (isset($_POST['Submit'])) {
    echo '<h1>Submitted!</h1>';
    // Undo the setting of the variable
    unset($_POST['Submit']);
?>
  <!--This form is just used to refresh the page, together with the unset above.-->
  <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <input type="submit" name="Redo" value="Redo" />
  </form>
<?php
  } else {
?>
  <!--This is the initial form-->
  <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <input type="submit" name="Submit" value="Submit the Form" />
  </form>
<?php
  }
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.