PHP Simple Form

This page provides a very simple PHP form example. The code shows how to make a self referencing form in PHP. A test is performed at the top of the script. If the HTTP request was a get the value gets set to "1" or true, otherwise $isGet is set to an empty value. The variable can then be used to see whether to process a get or a post.

testForm.php (Demo)
testForm.php (Source)

   1:<?php
   2:$isGet = ($_SERVER['REQUEST_METHOD'] == 'GET');
   3:?>
   4:<html>
   5:<head>
   6:  <title>Test Form</title>
   7:</head>
   8:<body>
   9:  <h2>Test Form</h2>
  10:  <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
  11:    <label>What is your message?</label>
  12:    <input type="text" name="message" maxlength="25" />
  13:    <br/>
  14:    <input type="submit" value="Send" />
  15:  </form>
  16:<p>
  17:<?php
  18:if (!($isGet)){
  19:    echo 'Message: ' . $_POST['message'];
  20:}
  21:?>
  22:  </p>
  23:</body>
  24:</html>