title: How to return to a certain section of a page when a form comes back with errors
body: <p>I have a one page website. Each menu item scrolls you to a different part of the page. I have a HTML and PHP form which reloads the page and displays an error message if the form is submitted with a required field not filled. The problem is it reloads to the top of the page whereas my form is at the bottom of the page.</p>
  
  <p>How can I get the form to reload the page to the section where the contact form is? The form already does exactly that when the form is submitted successfully but not when there are errors.</p>
  
  <p>Any help would be great. Thanks for reading.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;div id="contactBox"&gt;
  
              &lt;form name="contact" id="contactForm" method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
                  &lt;div id="formLeft"&gt;
                      &lt;p&gt;
                          &lt;label for="name"&gt;Name&lt;/label&gt;
                          &lt;?php if ($missing &amp;&amp; in_array('name', $missing)) { ?&gt;
                          &lt;div class="warningDivLeft"&gt;
                          &lt;span class="warning"&gt;Please enter your name&lt;/span&gt;
                          &lt;/div&gt;                             
                          &lt;?php } ?&gt;
                          &lt;input type="text" name="name" id="name" tabindex="10"
                          &lt;?php
                          if ($errors || $missing) {
                              echo 'value="' . htmlentities($name, ENT_COMPAT, 'utf-8') . '"';
                          }
                          ?&gt;      
                          &gt;
                      &lt;/p&gt;
                      &lt;p&gt;                           
                          &lt;label for="company"&gt;Company Name/Website&lt;/label&gt;
                          &lt;input type="text" name="company" id="company" tabindex="30"
                          &lt;?php
                          if ($errors || $missing) {
                              echo 'value="' . htmlentities($company, ENT_COMPAT, 'utf-8') . '"';
                          }
                          ?&gt;      
                          &gt;
                      &lt;/p&gt;
                      &lt;p&gt;                           
                          &lt;label for="email"&gt;Email&lt;/label&gt;
                          &lt;?php if ($missing &amp;&amp; in_array('email', $missing)) { ?&gt;
                          &lt;div class="warningDivLeft"&gt;
                          &lt;span class="warning"&gt;Please enter your email&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;?php } elseif (isset($errors['email'])) { ?&gt;
                          &lt;div class="warningDivLeft"&gt;
                          &lt;span class="warning"&gt;Invalid email address&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;?php } ?&gt;
                          &lt;input type="email" name="email" id="email" tabindex="40"
                          &lt;?php
                          if ($errors || $missing) {
                              echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"';
                          }
                          ?&gt;      
                          &gt;
                      &lt;/p&gt;
                      &lt;p&gt;                           
                          &lt;label for="phone"&gt;Phone&lt;/label&gt;
                          &lt;input type="text" name="phone" id="phone" tabindex="50"
                          &lt;?php
                          if ($errors || $missing) {
                              echo 'value="' . htmlentities($phone, ENT_COMPAT, 'utf-8') . '"';
                          }
                          ?&gt;      
                          &gt;
                      &lt;/p&gt;
                      &lt;p&gt;                           
                          &lt;label for="contactYou"&gt;Contact you by...&lt;/label&gt;
                          &lt;?php if ($missing &amp;&amp; in_array('contactYou', $missing)) { ?&gt;
                          &lt;div class="warningDivLeft"&gt;
                          &lt;span class="warning"&gt;Please select one&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;?php } ?&gt;
                          &lt;select name="contactYou" size="1" id="contactYou" tabindex="60"&gt;
                              &lt;option value="" selected="selected"&gt;- select&lt;/option&gt;
                              &lt;option value="email" &lt;?php echo ($contactYou == 'email') ? ' selected="selected"' : ''; ?&gt;&gt;Email&lt;/option&gt;
                              &lt;option value="phone" &lt;?php echo ($contactYou == 'phone') ? ' selected="selected"' : ''; ?&gt;&gt;Phone&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/p&gt; 
                  &lt;/div&gt;
                  &lt;div id="formRight"&gt;
                      &lt;p&gt;
                          &lt;label for="interest"&gt;I am interested in...&lt;/label&gt;
                          &lt;?php if ($missing &amp;&amp; in_array('interest', $missing)) { ?&gt;
                          &lt;div class="warningDiv"&gt;
                          &lt;span class="warning"&gt;Please select one&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;?php } ?&gt;
                          &lt;select name="interest" size="1" id="interest" tabindex="80"&gt;
                              &lt;option value="" selected="selected"&gt;- select&lt;/option&gt;
                              &lt;option value="new" &lt;?php echo ($interest == 'new') ? ' selected="selected"' : ''; ?&gt;&gt;Creating a new website&lt;/option&gt;
                              &lt;option value="current" &lt;?php echo ($interest == 'current') ? ' selected="selected"' : ''; ?&gt;&gt;Redesigning a current website&lt;/option&gt;
                              &lt;option value="responsive" &lt;?php echo ($interest == 'responsive') ? ' selected="selected"' : ''; ?&gt;&gt;Reponsive web design&lt;/option&gt;
                              &lt;option value="wordpress" &lt;?php echo ($interest == 'wordpress') ? ' selected="selected"' : ''; ?&gt;&gt;A WordPress website&lt;/option&gt;
                              &lt;option value="general" &lt;?php echo ($interest == 'general') ? ' selected="selected"' : ''; ?&gt;&gt;General enquiry&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/p&gt;
                      &lt;p&gt;
                          &lt;label for="budget"&gt;My budget is...&lt;/label&gt;
                          &lt;?php if ($missing &amp;&amp; in_array('budget', $missing)) { ?&gt;
                          &lt;div class="warningDiv"&gt;
                          &lt;span class="warning"&gt;Please select one&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;?php } ?&gt;
                          &lt;select name="budget" size="1" id="budget" tabindex="90"&gt;
                              &lt;option value="" selected="selected"&gt;- select&lt;/option&gt;
                              &lt;option value="100" &lt;?php echo ($budget == '100') ? ' selected="selected"' : ''; ?&gt;&gt;€100 - €500&lt;/option&gt;
                              &lt;option value="500" &lt;?php echo ($budget == '500') ? ' selected="selected"' : ''; ?&gt;&gt;€500 - €1,000&lt;/option&gt;
                              &lt;option value="1000" &lt;?php echo ($budget == '1000') ? ' selected="selected"' : ''; ?&gt;&gt;€1,000 - €2,000&lt;/option&gt;
                              &lt;option value="2000" &lt;?php echo ($budget == '2000') ? ' selected="selected"' : ''; ?&gt;&gt;€2,000 - €5,000&lt;/option&gt;
                              &lt;option value="5000" &lt;?php echo ($budget == '5000') ? ' selected="selected"' : ''; ?&gt;&gt;€5,000 - €10,000&lt;/option&gt;
                              &lt;option value="10000" &lt;?php echo ($budget == '10000') ? ' selected="selected"' : ''; ?&gt;&gt;€10,000+&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/p&gt;
                      &lt;p&gt;
                          &lt;label for="comments"&gt;How can I help you?&lt;/label&gt;
                          &lt;?php if ($missing &amp;&amp; in_array('comments', $missing)) { ?&gt;
                          &lt;div class="warningDiv"&gt;
                          &lt;span class="warning"&gt;Please leave a comment&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;?php } ?&gt;
                          &lt;textarea name="comments" id="comments" cols="45" rows="5" tabindex="100"&gt;&lt;?php
                          if ($errors || $missing) {
                              echo htmlentities($comments, ENT_COMPAT, 'utf-8');
                          }
                          ?&gt;&lt;/textarea&gt;
                      &lt;/p&gt;
                  &lt;/div&gt;
                  &lt;div id="formSubmit"&gt;
                      &lt;ul&gt;
                          &lt;li&gt;
                            &lt;input type="submit" name="submit" id="submit" value="Send Message" tabindex="70"&gt;
                          &lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
                      &lt;/form&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  $errors = array();
  $missing = array();
  if (isset($_POST['submit'])) {
      $to = 'celinehalpin@hotmail.com';
      $subject = 'Web Design';
      $expected = array('name', 'company', 'email', 'phone', 'contactYou', 'interest', 'budget', 'comments');
      $required = array('name', 'email', 'contactYou', 'interest', 'budget', 'comments');
      $headers = "From: webmaster@example.com\r\n";
      $headers .= "Content-type: text/plain; charset=utf-8";
      $authenticate = '-fcelinehalpin@hotmail.com';
      require './_includes/mail_process.php';
      if ($mailSent) {
          header('Location: ' . $_SERVER['REQUEST_URI'] . '?success=1#c');
          exit;   
      }
  }
  ?&gt;
  
  
  &lt;?php if (($_POST &amp;&amp; $suspect) || ($_POST &amp;&amp; isset($errors['mailfail']))) { ?&gt;
          &lt;div class="globalWarning"&gt;
          &lt;p class="warning"&gt;Sorry your mail could not be sent&lt;/p&gt;
          &lt;/div&gt;
  &lt;?php } elseif ($errors || $missing) { ?&gt;
          &lt;div class="globalWarning"&gt;
          &lt;p class="warning"&gt;Please fix the item(s) indicated&lt;/p&gt;
          &lt;/div&gt;
  &lt;?php } elseif (isset($_GET['success'])) { ?&gt;
          &lt;div class="globalAlert"&gt;
          &lt;p class="warning"&gt;Thank you! Your message has been sent!&lt;/p&gt;
          &lt;/div&gt;
  &lt;?php } ?&gt;
  
  &lt;?php
  $suspect = false;
  $pattern = '/Content-Type:|Bcc:|Cc:/i';
  
  function isSuspect($val, $pattern, &amp;$suspect) {
      if (is_array($val)) {
          foreach ($val as $item) {
              isSuspect($item, $pattern, $suspect);   
          }
      } else {
          if (preg_match($pattern, $val)) {
              $suspect = true;    
          }
      }
  }
  
  isSuspect($_POST, $pattern, $suspect);
  
  if (!$suspect) {
      foreach ($_POST as $key =&gt; $value) {
          $temp = is_array($value) ? $value : trim($value);
          if (empty($temp) &amp;&amp; in_array($key, $required)) {
              $missing[] = $key;
              $$key = '';
          } elseif(in_array($key, $expected)) {
              $$key = $temp;
          }
      }
  }
  
  if (!$suspect &amp;&amp; !empty($email)) {
      $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
      if ($validemail) {
          $headers .= "\r\nReply-to: $validemail";
      } else {
          $errors['email'] = true;
      }
  }
  
  if (!$suspect &amp;&amp; !$missing &amp;&amp; !$errors) {
      $message = '';
      foreach ($expected as $item) {
          if (isset($$item) &amp;&amp; !empty($$item)) {
              $val = $$item;
          } else {
              $val = 'Not selected';
          }
          if (is_array($val)) {
              $val = implode(', ', $val); 
          }
          $item = str_replace(array('_', '-'), ' ', $item);
          $message .= ucfirst($item) . ": $val\r\n\r\n";
      }
      $message = wordwrap($message, 70);
  
      $mailSent = mail($to, $subject, $message, $headers, $authenticate);
      if (!$mailSent) {
          $errors['mailfail'] = true;
      }
  }
  </code></pre>