title: How to automatically focus cursor on input field after PHP validation
body: <p>How can I automatically focus the cursor on an input field after PHP validation. If user enters an incorrect answer, I want to focus on that field when the page loads.</p>
  
  <p>I'm new to PHP. How can I do this in PHP?</p>
  
  <h1>Contact Form:</h1>
  
  <pre><code>&lt;form method="post" action="&lt;?php htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;
      &lt;fieldset style="width:960px; background-color:#FFF; border-radius:10px; padding:50px;"&gt;
          &lt;legend&gt;&lt;h2&gt;Contact Us&lt;/h2&gt;&lt;/legend&gt;
          &lt;br /&gt;
          &lt;fieldset style="border-radius:10px; padding:50px 0px 50px 50px;;";&gt;
              &lt;div style="width:275px; height:300px; float:right; padding:30px"&gt;&lt;img src="images/contactus.jpg" width="275" height="300" alt="contactus" /&gt;&lt;/div&gt;
              &lt;label&gt;Name:&lt;/label&gt;
              &lt;input class="txt" type="text" name="name" value="&lt;?php echo isset($_POST['name']) ? htmlentities($_POST['name']) : ''; ?&gt;"/&gt;&lt;span class="error"&gt;&lt;?php echo $nameErr?&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;label&gt;Email:&lt;/label&gt;
              &lt;input class="txt" type="text" name="email" value="&lt;?php echo isset($_POST['email']) ? htmlentities($_POST['email']) : ''; ?&gt;"/&gt;&lt;span class="error"&gt;&lt;?php echo $emailErr?&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;label&gt;Mobile No:&lt;/label&gt;
              &lt;input class="txt" type="text" name="mobileno" value="&lt;?php echo isset($_POST['mobileno']) ? htmlentities($_POST['mobileno']) : ''; ?&gt;"/&gt;&lt;span class="error"&gt;&lt;?php echo $mobilenoErr?&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;label&gt;Message:&lt;/label&gt;
              &lt;textarea class="txt" name="message"rows="5"&gt;&lt;?php echo $message?&gt;&lt;/textarea&gt;&lt;span class="error"&gt;&lt;?php echo $messageErr?&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;input style="margin-left:100px; padding:5px 20px 5px 20px; border-radius:5px" type="submit" value = "Submit" /&gt;
          &lt;/fieldset&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>
  
  <h1>Contact form validation in PHP:</h1>
  
  <pre><code>&lt;?php
  if(isset($_POST['name']))
  {
      include_once("config.php");
  
      $name=$email=$mobileno=$message=="";
      $nameErr=$emailErr=$mobilenoErr=$messageErr="";
  
      function test_input($data)
      {
          $data=trim($data);
          $data-stripslashes($data);
          $data=htmlspecialchars($data);
          return $data;
      }
  
      if($_SERVER['REQUEST_METHOD']=="POST")
      {
          $valid=true;
  
          //name validaton
          if(empty($_POST["name"]))
          {
              $nameErr="* Name is Required";
              $valid=false;
          }
          else
          {
              $name=test_input($_POST["name"]);
              if (!preg_match("/^[a-zA-Z ]*$/",$name))
              {
                  $nameErr = "&amp;nbsp;&amp;nbsp;Only letters and white space allowed";
                  $valid=false;
              }
          }
  
          //Email Address validaton
          if(empty($_POST["email"]))
          {
              $emailErr="* Email is Required";
              $valid=false;
          }
          else
          {
              $email=test_input($_POST["email"]);
              if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
              {
                  $emailErr="&amp;nbsp;&amp;nbsp; Enter a valid Email ID";
                  $valid=false;
              }
          }
  
          //Mobile no validaton
          if(empty($_POST["mobileno"]))
          {
              $mobilenoErr="* Mobile no is Required";
              $valid=false;
          }
          else
          {
              $mobileno=test_input($_POST["mobileno"]);
              if(!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$mobileno))
              {
                  $mobilenoErr="*Enter a valid contact no";
                  $valid=false;
              }
          }
  
          //bank name validation
          if(empty($_POST["message"]))
          {
              $messageErr="* Message is Required";
              $valid=false;
          }
          else
          {
              $message=test_input($_POST["message"]);
          }
      }
  
      if($valid)
      {
          echo "Send mail code";
      }
  }
  ?&gt;
  </code></pre>