title: Require form fields with php
body: <p>Given the following code example, what would I need to add to the check_input function so that it deals with missing / required form fields. Basically, all I am trying to do is to show the end user an error message on the top of my form that says something like "Fields marked with a * are required" if they try to submit the form without filling out all the required fields.</p>
  
  <p>Any help would be greatly appreciated and thank in advance for your time.</p>
  
  <pre><code> // Don't post the form until the submit button is pressed.
  if(isset($_POST['submit'])) {
  
    echo( 
     check_input($_POST['name']) . &lt;br&gt; .
     check_input($_POST['city']);
  
  }
  
  // check_input function
  function check_input($data)
  {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data, ENT_QUOTES);
    return $data;
  }
  </code></pre>
  
  <p>The Form</p>
  
  <pre><code>&lt;form action="test.php" method="post"&gt;
    &lt;input type="text" name="name"&gt;
    &lt;input type="text" name="city"&gt;
    &lt;input type="submit" name="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>