title: PHP - Display error message inside text field if form fails validation
body: <p>Hi please excuse the rookie question as I am still very much learning PHP. Im trying to do a basic form validation. Im trying to get any <strong>errors found upon validation to be displayed in red inside the form field.</strong> If no errors are found the form should redirect to a Thank you page.</p>
  
  <p>Ive written a basic validation, but I am now stuck on how to correctly implement it. </p>
  
  <p>My question is:</p>
  
  <ol>
  <li>How do I get the error messages to display inside the form field WITHOUT sending the form?</li>
  <li>How do I procees to send the form if no errors is found?</li>
  </ol>
  
  <p>My code follows:</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;form id="enquire" method="post" action="thankyou.php"&gt;
          &lt;h2&gt;Test Drive an Audi Today&lt;/h2&gt;
          &lt;input type="text"  value="Name" class="textbox" id="name" name="name" onfocus="if(this.value=='Name') this.value='';" /&gt;&lt;br /&gt;
          &lt;br /&gt;
          &lt;input type="text" value="Surname" class="textbox" id="surname" name="surname"  onfocus="if(this.value=='Surname') this.value='';"  /&gt;&lt;br /&gt;
          &lt;br /&gt;
          &lt;input type="text" value="Contact Number" class="textbox" id="nr" name="nr" onfocus="if(this.value=='Contact Number') this.value='';"   /&gt; &lt;br /&gt;
          &lt;br /&gt;
          &lt;input type="text" value="Email" class="textbox" id="email" name="email"onfocus="if(this.value=='Email') this.value='';" /&gt;&lt;br /&gt;
          &lt;br /&gt;
  
      &lt;select id="swapImg" name="model" class="modelSelect" onchange="swapImage()" &gt;
         &lt;option value="images/1.jpg"&gt;A4&lt;/option&gt;
      &lt;option value="images/2.jpg" selected="selected"&gt;A6&lt;/option&gt;
      &lt;option value="images/3.jpg"&gt;A8&lt;/option&gt;
      &lt;/select&gt;
          &lt;br /&gt;
          &lt;br /&gt;
            &lt;input type="submit" name="submit" class="butt" value="Send"  /&gt;
             &lt;span class="buttonText"&gt;We'll Call you Back!&lt;/span&gt;
          &lt;/form&gt; 
  </code></pre>
  
  <p><strong>PHP</strong></p>
  
  <pre><code> if (isset($_POST['enquire'])){
                //condition
            $correct = false;
  
              //Get Info
          $name =  $_POST['name'];
          $surname = $_POST['surname'];
          $number = $_POST['nr'];
          $email = $_POST['email'];
          $_REQUEST['model'];
  
              //name fields 
              $string_exp = "/^[A-Za-z .'-]+$/";
              //email field
              $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  
              //validate name field
              if($name == ""){
                  $name_error="* Please enter your name";
                  $correct = false;
              }
  
              else if (!preg_match($string_exp,$name)) {
              $name_error= "* Wrong Format";
              $correct= false;
              }
  
              //validate surname 
              if($surname == ""){
                  $surname_error= "* Please enter your surname.";
                  $correct= false;
              }
              else if(!preg_match($string_exp,$surname)){
                  $surname_error= "* Wrong Format";
                  $correct = false;
              }
              //check email
              if($email==""){
                  $email_error="* Please enter your email";
                  $correct = false;
              }
              else if(!preg_match($email_exp,$email)){
                  $email_error = "* Wrong Format";
                  $correct = false;
              }
  
              //check number
              if($number=""){
                  $phone_error = "* Please enter your phone number";
                  $correct = false;
              }
              else if(!is_numeric($number)){
              $phone_error ='* Please enter only numbers 0-9.';
              $correct = false;  
               }
  
               //proceed if all fields validate
          if($correct){
          $email_to  = "tim@yah.co";
          $subject ="New Enquiry";
  
              //set message 
        $email_message .= "First Name: ".($name)."\n";
        $email_message .= "Last Name: ".($lastname)."\n";
        $email_message .= "Email: ".($email)."\n";
        $email_message .= "Telephone: ".($telephone)."\n"; 
        $email_message .= "Model: ".($model)."\n";
  
         // create email headers
          $headers = 'From: '.$email."\r\n".
          'Reply-To: '.$email."\r\n" .
          @mail($email, $email_subject, $email_message, $headers);  
  
          header("thankyou.php");
          exit();
  
               }//end if
              }// end isset
  </code></pre>
  
  <p>if anyone could point me in the right direction it would be greatly appreciated.</p>