title: Php Thank you Message in same page
body: <p>I have to validate a form and send an email..I copied this form from w3schools and attached mailto function.But i dont know how to display "Thank you for submitting" After the form is processed. Kindly Help.. The Thank you message should display on the same page below Submit Button.</p>
  
  <p>Here is my code..</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt; 
  &lt;html&gt;
      &lt;head&gt;
          &lt;style&gt;
              .error {color: #FF0000;}
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt; 
  
      &lt;?php
      // define variables and set to empty values
      $nameErr = $emailErr = $genderErr = $websiteErr = "";
      $name = $email = $gender = $comment = $website = "";
  
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
         if (empty($_POST["name"])) {
           $nameErr = "Name is required";
         } else {
           $name = test_input($_POST["name"]);
           // check if name only contains letters and whitespace
           if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
             $nameErr = "Only letters and white space allowed"; 
           }
         }
  
         if (empty($_POST["email"])) {
           $emailErr = "Email is required";
         } else {
           $email = test_input($_POST["email"]);
           // check if e-mail address is well-formed
           if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             $emailErr = "Invalid email format"; 
           }
         }
  
         if (empty($_POST["website"])) {
           $website = "";
         } else {
           $website = test_input($_POST["website"]);
           // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
           if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i",$website)) {
             $websiteErr = "Invalid URL"; 
           }
         }
  
         if (empty($_POST["comment"])) {
           $comment = "";
         } else {
           $comment = test_input($_POST["comment"]);
         }
  
         if (empty($_POST["gender"])) {
           $genderErr = "Gender is required";
         } else {
           $gender = test_input($_POST["gender"]);
         }
      }
  
      function test_input($data) {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
      }
  
  
      ?&gt;
  
      &lt;h2&gt;PHP Form Validation Example&lt;/h2&gt;
      &lt;p&gt;&lt;span class="error"&gt;* required field.&lt;/span&gt;&lt;/p&gt;
      &lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt; 
         Name: &lt;input type="text" name="name" value="&lt;?php echo $name;?&gt;"&gt;
         &lt;span class="error"&gt;* &lt;?php echo $nameErr;?&gt;&lt;/span&gt;
         &lt;br&gt;&lt;br&gt;
         E-mail: &lt;input type="text" name="email" value="&lt;?php echo $email;?&gt;"&gt;
         &lt;span class="error"&gt;* &lt;?php echo $emailErr;?&gt;&lt;/span&gt;
         &lt;br&gt;&lt;br&gt;
         Website: &lt;input type="text" name="website" value="&lt;?php echo $website;?&gt;"&gt;
         &lt;span class="error"&gt;&lt;?php echo $websiteErr;?&gt;&lt;/span&gt;
         &lt;br&gt;&lt;br&gt;
         Comment: &lt;textarea name="comment" rows="5" cols="40"&gt;&lt;?php echo $comment;?&gt;&lt;/textarea&gt;
         &lt;br&gt;&lt;br&gt;
         Gender:
         &lt;input type="radio" name="gender" &lt;?php if (isset($gender) &amp;&amp; $gender=="female") echo "checked";?&gt;  value="female"&gt;Female
         &lt;input type="radio" name="gender" &lt;?php if (isset($gender) &amp;&amp; $gender=="male") echo "checked";?&gt;  value="male"&gt;Male
         &lt;span class="error"&gt;* &lt;?php echo $genderErr;?&gt;&lt;/span&gt;
         &lt;br&gt;&lt;br&gt;
         &lt;input type="submit" name="submit" value="Submit"&gt; 
      &lt;/form&gt;
  
  
      &lt;?php
      $to = "dheeraj.narayan1712@gmail.com";
      $subject = "My subject";
      $name = "Name: $name";
      $email = "$email";
      $headers = "From: webmaster@example.com" . "\r\n" .
      "CC: somebodyelse@example.com";
  
      mail($to,$subject,$name,$headers);
      ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>