title: Change a field in PHP form from required to optional
body: <p>I am a complete novice when it comes to PHP. I downloaded a form I found online to include in my Bootstrap site. It is a very simple form anyone can use to send me a message. I managed to set up Wamp to test out the PHP but when I leave the Phone field blank it gives me an error message telling me please go back and correct the error. I want to make it so if someone leaves out the phone number it still sends the email. Thank you.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;form name="contactform" method="post" action="index.php" class="form-vertical"&gt;
        &lt;div class="form-group"&gt;
            &lt;label for="inputName" class="control-label"&gt;Name&lt;/label&gt;
                &lt;input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last"&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
            &lt;label for="inputEmail" class="control-label"&gt;Email*&lt;/label&gt;
            &lt;input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required"&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
          &lt;label for="inputPhone" class="control-label"&gt;Phone Number&lt;/label&gt;
          &lt;input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional"&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
          &lt;label for="inputMessage" class="control-label"&gt;Message&lt;/label&gt;
          &lt;textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"&gt;&lt;/textarea&gt;
        &lt;/div&gt;
        &lt;div class="form-group"&gt;
          &lt;button type="submit" class="btn btn-custom pull-right hvr-underline-from-left"&gt;Send&lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
      /* Set e-mail recipient */
      $myemail = "myaccount@gmail.com";
  
      /* Check all form inputs using check_input function */
      $name = check_input($_POST['inputName'], "First and Last");
      $email = check_input($_POST['inputEmail'], "Required");
      $phone = check_input($_POST['inputPhone'], "Optional");
      $message = check_input($_POST['inputMessage'], "Brief Description");
  
      /* If e-mail is not valid show error message */
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
          show_error("Invalid e-mail address");
      }
      /* Let's prepare the message for the e-mail */
  
      $subject = "Contact Message from mywebsite.net";
  
      $message = "
  
      Someone has sent you a message using your contact form:
  
      Name: $name
      Email: $email
      Phone: $phone
  
      Message:
      $message
  
      ";
  
      /* Send the message using mail() function */
      mail($myemail, $subject, $message);
  
      /* Redirect visitor to the thank you page */
      header('Location:contact.html');
      exit();
  
      /* Functions we used */
      function check_input($data, $problem='')
      {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          if ($problem &amp;&amp; strlen($data) == 0)
          {
              show_error($problem);
          }
          return $data;
      }
  
      function show_error($myError)
      {
  ?&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;p&gt;Please correct the following error:&lt;/p&gt;
  &lt;strong&gt;&lt;?php echo $myError; ?&gt;&lt;/strong&gt;
  &lt;p&gt;Hit the back button and try again&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
          exit();
      }
  ?&gt;
  </code></pre>