title: How to display validation error next to the field?
body: <p>I am trying to create a simple form for a wordpress site. I've created a full form and it's working fine. The code is below.</p>
  
  <pre><code>&lt;?php
  //Setup an empty array.
  $errors = array(); 
      if($_POST["submit"]) {
      $to = "myemail@gmail.com";
      $subject = "New reservations request";
      $hotel = $_POST["hotel_url"];
      $sender = $_POST["sendername"];
      $senderEmail = $_POST["senderEmail"];
  
      //Check the name and make sure that it isn't a blank/empty string.
      if(strlen(trim($sender)) === 0){
          //Blank string, add error to $errors array.
          $errors[] = "You must enter your name!";
      }
  
      //Make sure that the email address is valid.
      if(!filter_var($senderEmail, FILTER_VALIDATE_EMAIL)) {
          //$email is not a valid email. Add error to $errors array.
          $errors[] = "That is not a valid email address!";
      }
  
      if(!empty($errors)){ 
          echo '&lt;h1 style="color: red"&gt;Error(s)!&lt;/h1&gt;';
          foreach($errors as $errorMessage){
              echo $errorMessage . '&lt;br&gt;';
          }
      } 
  
      if(empty($errors)){
  
          $mailBody = "&lt;table border='1'&gt;
                         &lt;tr&gt;
                          &lt;th&gt;No&lt;/td&gt;
                          &lt;th&gt;Item&lt;/td&gt;
                          &lt;th&gt;Description&lt;/td&gt;
                         &lt;/tr&gt;
                         &lt;tr&gt;
                          &lt;td&gt;01&lt;/td&gt;
                          &lt;td&gt;Hotel&lt;/td&gt;
                          &lt;td&gt;$hotel&lt;/td&gt;
                         &lt;/tr&gt;
                         &lt;tr&gt;
                          &lt;td&gt;02&lt;/td&gt;
                          &lt;td&gt;Name&lt;/td&gt;
                          &lt;td&gt;$sender&lt;/td&gt;
                         &lt;/tr&gt;
                         &lt;tr&gt;
                          &lt;td&gt;03&lt;/td&gt;
                          &lt;td&gt;E-Mail&lt;/td&gt;
                          &lt;td&gt;$senderEmail&lt;/td&gt;
                         &lt;/tr&gt;
                      &lt;/table&gt;";
  
              $headers = array('From: '.$_POST['sendername'].' &lt;'.$_POST['senderEmail'].'&gt;');
  
              $mail_sent = wp_mail( $to, $subject, $mailBody, $headers ); 
          }
      }
  
      if ($mail_sent) {
  ?&gt;
      &lt;p&gt;Request sent&lt;/p&gt;
  
  &lt;?php 
  } else {
  ?&gt;
  
  &lt;form id="" name="" action="&lt;?php echo get_permalink(); ?&gt;" method="post"&gt;
  &lt;input type="hidden" name="hotel_url" value="&lt;?php echo get_permalink();?&gt;" /&gt;
  
      &lt;div class="section-heading"&gt;&lt;h6&gt;Your Details&lt;/h6&gt;&lt;/div&gt;    
      &lt;div class="label-input-wrapper"&gt;
          &lt;div class="form-label"&gt;Name&lt;/div&gt;&lt;div class="form-input"&gt;&lt;input type="text" name="sendername"/&gt;&lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="label-input-wrapper"&gt;
          &lt;div class="form-label"&gt;E-Mail&lt;/div&gt;&lt;div class="form-input"&gt;&lt;input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required /&gt;&lt;/div&gt;
      &lt;/div&gt;  
  
      &lt;input type="submit" value="Submit" name="submit"&gt;
  &lt;/form&gt;
  
  &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p>This code snippet sends the user input as a HTML table to the email. Validation also works fine but the validation errors appear at top of the form. </p>
  
  <p><img src="https://i.stack.imgur.com/8uaE1.jpg" alt="see the screen shot"></p>
  
  <p>I want to show the validation errors next to the each field. How can do that using <strong>PHP</strong> only?</p>