title: PHP form validation (don't know how to split it in two different pages)
body: <p>I am working on a contact form in PHP. My knowledge of PHP is pretty much non-existent. </p>
  
  <p>I've tried for a while to get a HTML form to submit to PHP form to have its text fields validated and required if blank but couldn't get anything to work. I also don't know AJAX, otherwise, would have attempted that. </p>
  
  <p>So, have resorted to having a PHP self-form inside the HTML page. </p>
  
  <p>This is the current version: </p>
  
  <pre><code>&lt;?php
  // define variables and set to empty values
  $firstNameErr = $lastNameErr = $emailErr = $messageErr = "";
  $first_name = $last_name = $email = $message = "";
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (empty($_POST["first-name"])) {
       $firstNameErr = "First name is required";
     } else {
       $first_name = test_input($_POST["first-name"]);
     }
  
      if (empty($_POST["last-name"])) {
       $lastNameErr = "Last name is required";
     } else {
       $last_name = test_input($_POST["last-name"]);
     }
  
     if (empty($_POST["email"])) {
       $emailErr = "Email is required";
     } else {
       $email = test_input($_POST["email"]);
     }
  
     if (empty($_POST["message"])) {
       $messageErr = "Message is required";
     } else {
       $message = test_input($_POST["message"]);
     }
  }
  
  function test_input($data) {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
  }
  ?&gt;
  </code></pre>
  
  <p>This is the form on the same page: </p>
  
  <pre><code>&lt;form class="ui form"  method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
    &lt;div class="field"&gt;
      &lt;label&gt;First Name&lt;/label&gt;
      &lt;input name="first-name" id="first-name" placeholder="First Name" type="text"&gt;
       &lt;span class="error"&gt;* &lt;?php echo $firstNameErr;?&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;label&gt;Last Name&lt;/label&gt;
      &lt;input name="last-name" id="last-name" placeholder="Last Name" type="text"&gt;
       &lt;span class="error"&gt;* &lt;?php echo $lastNameErr;?&gt;&lt;/span&gt;
    &lt;/div&gt;
      &lt;div class="field"&gt;
      &lt;label&gt;Email&lt;/label&gt;
      &lt;input name="email" id="email" placeholder="Email" type="email"&gt;
       &lt;span class="error"&gt;* &lt;?php echo $emailErr;?&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;label&gt;Message&lt;/label&gt;
      &lt;textarea rows="2" placeholder="Please type in your message" name="message" id="message"&gt;&lt;/textarea&gt;
       &lt;span class="error"&gt;* &lt;?php echo $messageErr;?&gt;&lt;/span&gt;
    &lt;/div&gt;
  
    &lt;button class="ui button" type="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Is there a way to do this in two different pages, ie, form in first page, and carried over to the second page in sessions mode? Or is this acceptable for a commercial site? Please let me know. Thank you. </p>
  
  <p><strong>Addendum</strong></p>
  
  <pre><code>   if (empty($_POST["email"])) {
       $emailErr = "Email is required";
     } else {
        $email = filter_vars(test_input($_POST["email"], FILTER_VALIDATE_EMAIL));
     }
  </code></pre>
  
  <p><strong>Addendum 2</strong></p>
  
  <p>I have included this at the bottom of the php stuff, ie, after the function test_input.<code>header('location: php_mailer_form.php');</code> Is that the correct placement of it? </p>
  
  <p>But for some reason when I attempt to visit the contact.php form it doesn't even show up, it just goes straight to the error form which I have at the bottom of the php_mailer_form.php. </p>
  
  <pre><code> if(!$mail-&gt;send()) {
          header('location: url/contactError.html');
  
      } else {
          header('location: url/contactResult.html');
  
      }
  </code></pre>
  
  <p>Why? (please let me know if I need to include additional information). </p>
  
  <p><strong>Addendum 3</strong></p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  $first_name = $_SESSION['first-name'];
  $last_name = $_SESSION['last-name'];
  $email = $_SESSION['email'];
  $message = nl2br($_SESSION['message']);
  
  require 'PHPMailerAutoload.php';
  
  $mail = new PHPMailer;
  
  //$mail-&gt;SMTPDebug = 3;                               // Enable verbose debug output
  
  $mail-&gt;isSMTP();                                      // Set mailer to use SMTP
  $mail-&gt;Host = 'host_specified';  // Specify main and backup SMTP servers
  $mail-&gt;SMTPAuth = true;                               // Enable SMTP authentication
  $mail-&gt;Username = 'email_specified';                 // SMTP username
  $mail-&gt;Password = 'password_specified';                           // SMTP password
  $mail-&gt;SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
  $mail-&gt;Port = 587; 
  
  $mail-&gt;addReplyTo( $email, $first_name );
  $mail-&gt;addAddress( $email, $first_name );
  $mail-&gt;addAddress( 'email_specified', 'Staff' );
  $mail-&gt;From = 'email_specified';
  $mail-&gt;FromName = 'Staff';
  
  
  $mail-&gt;isHTML(true);                                  // Set email format to HTML
  
  $mail-&gt;Subject = 'Hotel Room Request';
  $mail-&gt;Body    = $message; 
  
  $mail-&gt;AltBody = 'To view the message, please use an HTML compatible email viewer!';
  
  if(!$mail-&gt;send()) {
      header('location: url/contactError.html');
  
  } else {
      header('location: url/contactResult.html');
  
  }
  </code></pre>
  
  <p><strong>Addendum 4</strong></p>
  
  <pre><code>&lt;!doctype html&gt;
  
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
  
    &lt;title&gt;Contact&lt;/title&gt;
  
  
  
    &lt;!--[if lt IE 9]&gt;
    &lt;script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;
    &lt;![endif]--&gt;
    &lt;style type="text/css"&gt;
      .ui.fixed.sticky + p {
          margin-top: 39px;
        }
  
        .error {
          color: #FF0000;
        }
  
    &lt;/style&gt;
  &lt;/head&gt;
  
  
  &lt;body&gt;
  
  &lt;?php
  session_start(); //allows use of session variables
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if (!isset($_POST["first-name"])) {
       $firstNameErr = "First name is required";
     } else {
       $first_name = test_input($_POST["first-name"]);
     }
  
      if (!isset($_POST["last-name"])) {
       $lastNameErr = "Last name is required";
     } else {
       $last_name = test_input($_POST["last-name"]);
     }
  
     if (!isset($_POST["email"])) {
       $emailErr = "Email is required";
     } else {
       $email = test_input($_POST["email"]);
     }
  
     if (!isset($_POST["message"])) {
       $messageErr = "Message is required";
     } else {
       $message = test_input($_POST["message"]);
     }
  
     if(isset($first_name) &amp;&amp; isset($last_name) &amp;&amp; isset($email) &amp;&amp; isset($message))
     {
       $_SESSION['first_name'] = $first_name;
       $_SESSION['last_name'] = $last_name;
       $_SESSION['email'] = $email;
       $_SESSION['message'] = $message;
  
       header("Location: contact9Sessions.php");
     }
  }
  
  function test_input($data) {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
  }
  ?&gt;
  
  &lt;div class="ui container"&gt;
  &lt;div class="ui segment"&gt;
    &lt;div&gt;
  
      &lt;div class="ui fluid five item tabular menu"&gt;
        &lt;a class="item" href="index.html"&gt;Home&lt;/a&gt;
        &lt;a class="item" href="about.html"&gt;About&lt;/a&gt;
        &lt;a class="item" href="rooms.html"&gt;Rooms Info &amp; Rates&lt;/a&gt;
        &lt;a class="item" href="book.html"&gt;To Book&lt;/a&gt;
        &lt;a class="item" href="contact.html"&gt;Contact&lt;/a&gt;
      &lt;/div&gt;
  
    &lt;/div&gt;
  
  &lt;div class="ui two column stackable grid"&gt;
  
  &lt;div class="ten wide column"&gt;
  
  &lt;form class="ui form"  method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
    &lt;div class="field"&gt;
      &lt;label&gt;First Name&lt;/label&gt;
      &lt;input name="first-name" id="first-name" placeholder="First Name" type="text"&gt;
       &lt;?php if(isset($firstNameErr)) print ('&lt;span class="error"&gt;* ' . $firstNameErr . '&lt;/span&gt;'); ?&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;label&gt;Last Name&lt;/label&gt;
      &lt;input name="last-name" id="last-name" placeholder="Last Name" type="text"&gt;
       &lt;?php if(isset($lastNameErr)) print ('&lt;span class="error"&gt;* ' . $lastNameErr . '&lt;/span&gt;'); ?&gt;
    &lt;/div&gt;
      &lt;div class="field"&gt;
      &lt;label&gt;Email&lt;/label&gt;
      &lt;input name="email" id="email" placeholder="Email" type="email"&gt;
       &lt;?php if(isset($emailErr)) print ('&lt;span class="error"&gt;* ' . $emailErr . '&lt;/span&gt;'); ?&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;label&gt;Message&lt;/label&gt;
      &lt;textarea rows="2" placeholder="Please type in your message" name="message" id="message"&gt;&lt;/textarea&gt;
       &lt;?php if(isset($messageErr)) print ('&lt;span class="error"&gt;* ' . $messageErr . '&lt;/span&gt;'); ?&gt;
    &lt;/div&gt;
  
    &lt;button class="ui button" type="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
    &lt;/div&gt;
  
    &lt;div class="six wide column"&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;img class="ui centered large bordered rounded image" src="images/tobereplaced.jpg"&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;div class="ui two column grid"&gt;
    &lt;div class="ui left aligned "&gt;
      &lt;p&gt;Left Footer Stuff Here&lt;/p&gt;
    &lt;/div&gt;
  
    &lt;div class="ui right aligned"&gt;
      &lt;p&gt;Right Footer Stuff Here&lt;/p&gt;
    &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>