title: PHP: Using isset to validate a form with required text field
body: <p>I've been trying to set up something which will check an HTML form, to make sure that the text fields have at least 1 character entered, before the form is submitted (to the next page).</p>
  
  <p>The HTML form is on one page, and flows through to a 2nd page. The 2nd page then updates a text file. That all works fine as is, so I dont want to change it. But what I do need is the validator. </p>
  
  <p>I've searched and read through numerous suggestions, have seen some suggestions for using ISSET, and done the script below.</p>
  
  <p>but this doesnt work (because the form is always submitted / there are no error messages).  I have tired numerous other suggestions (based on other tutorials I have read). But it seems, that regardless of whether the text fields are empty or not, the form will still always submit. (there is a timestamp on the next page, so I can tell what gets submitted.</p>
  
  <p>Can someone tell me whether Isset will work in this case? If so, where I am going wrong. Or if not, what I can use to get a validator for the text fields. I'd rather use PHP for this, but if I need to use Javascript that's a possibility. </p>
  
  <p>(Added: I think the problem is that even if the below $error messages occur, it will never show up, because the page is automatically redirected via the submit button. What I need it similar to a event handler - eg, if button submit is clicked, check for errors. If there are errors, print them. If not, submit form. this code doesnt do that).</p>
  
  <p>Page 1 code.</p>
  
  <pre><code>&lt;?php
  if (isset($_POST['submit'])) {
      $error = '';
      if (trim($_POST['topic']) == '') {
          $error = "Please enter a topic&lt;br&gt;");
      }
      if (trim($_POST['outline']) == '') {
          $error = "Please enter an outline&lt;br&gt;";
      }
      if ($error == '') {
          Echo "There are no errors";
      } else {
          echo "$error";
      }
  }
  ?&gt;
  &lt;form style="" method="post" action="addtopic2.php"&gt;
      Topic:&lt;input name="topic" id="topicbox" maxlength="100" type="text"&gt;
      Outline: &lt;textarea input wrap="nowrap" rows="10" cols="120" name="outline"&gt;
  &lt;/textarea&gt;&lt;br&gt;&lt;input name="submit" value="Submit" type="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Page 2 code</p>
  
  <pre><code> &lt;?php
   $b = time (); 
   $c = date("g:i:s A D, F jS Y",$b);
   print "Topic and Outline Submitted at $c";
  
   $t = "Topic:";
   $o = "Outline:";
   $d = "Time Date:";
  
   $topic = $_POST['topic'];
   $outline = $_POST['outline'];
  
   $data = stripslashes("$t $topic | $o $outline | $d $c |\n");
  
   $fh = fopen("users.txt", "a");
   fwrite($fh, $data); 
   fclose($fh); 
   ?&gt;
  </code></pre>