title: Submit contact form with success/fail alert on same page
body: <p>I have contact form with html, php, js and ajax as you see in codes. After form fill successfully and click submit, the email was sent successfully. But the success message can't seen on same page. It see another page. Here is the codes. Could you help me what I miss or do wrongly. Thank you for your help.</p>
  
  <p><strong>--- HTML CODE ---</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
   &lt;div class="row"&gt;
    &lt;div class="col-xl-8 offset-xl-2"&gt;
     &lt;h1&gt;CONTACT FORM&lt;/h1&gt;&lt;hr&gt;
     &lt;p class="lead"&gt;By filling out the contact form; You may have information about us and current news.&lt;/p&gt;
     &lt;form id="contact-form" method="post" action="contact.php" role="form" novalidate="true"&gt;
     &lt;div class="messages"&gt;&lt;/div&gt;
     &lt;div class="controls"&gt;
      &lt;div class="row"&gt;
       &lt;div class="col-lg-6"&gt;
        &lt;div class="form-group"&gt;
          &lt;label for="form_name"&gt;Full Name *&lt;/label&gt;
          &lt;input id="form_name" type="text" name="name" class="form-control" placeholder="Please fill the name field *" required="required" data-error="You must fill the name field"&gt;
          &lt;div class="help-block with-errors alert-danger"&gt;&lt;/div&gt;
         &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-lg-6"&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
       &lt;div class="col-lg-6"&gt;
        &lt;div class="form-group"&gt;
         &lt;label for="form_email"&gt;E-mail *&lt;/label&gt;
         &lt;input id="form_email" type="email" name="email" class="form-control" placeholder="Please fill the email field *" required="required" data-error="You must fill the email field"&gt;
          &lt;div class="help-block with-errors alert-danger"&gt;&lt;/div&gt;
         &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-lg-6"&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
       &lt;div class="col-lg-6"&gt;
        &lt;div class="form-group"&gt;
         &lt;label for="form_subject"&gt;Subject *&lt;/label&gt;
         &lt;input id="form_subject" type="text" name="subject" class="form-control" placeholder="Please fill the subject field *" required="required" data-error="You must fill the subject field"&gt;
          &lt;div class="help-block with-errors alert-danger"&gt;&lt;/div&gt;
         &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-lg-6"&gt;&lt;/div&gt;
       &lt;/div&gt;
       &lt;div class="form-group"&gt;
        &lt;label for="form_message"&gt;Message *&lt;/label&gt;
        &lt;textarea id="form_message" name="message" class="form-control" placeholder="Please fill the message field *" rows="4" required="required" data-error="You must fill the message field"&gt;&lt;/textarea&gt;
         &lt;div class="help-block with-errors alert-danger"&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;input type="submit" class="btn btn-success btn-send" value="Submit"&gt;
        &lt;p class="text-muted" style="padding-top: 5px;"&gt;&lt;strong&gt;*&lt;/strong&gt;This field must be fill.&lt;/p&gt;
        &lt;/div&gt;&lt;!-- controls all end --&gt;
       &lt;/form&gt;&lt;!-- form all end --&gt;
      &lt;/div&gt;&lt;!-- col-xl-8 offset-xl-2 end --&gt;
     &lt;/div&gt;&lt;!-- row all end --&gt;
   &lt;/div&gt;&lt;!-- container all end --&gt;
  </code></pre>
  
  <p><strong>--- PHP CODE ---</strong></p>
  
  <pre><code>$from = '';
  $sendTo = 'email@email.com';
  $subject = 'New message from contact form';
  $fields = array('name' =&gt; 'Name', 'email' =&gt; 'Email', 'subject' =&gt; 'Subject', 'message' =&gt; 'Message');
  
  $okMessage = 'Thank you for your message. I will write back soon.';
  $errorMessage = 'There is an error while sending message. Please try again later.';
  
  try {if (!empty($_POST)) {
      $emailText = "You have a new message from your contact form\n=====\n";
      foreach ($_POST as $key =&gt; $value) {
         if (isset($fields[$key])) {
           $emailText .= "$fields[$key]: $value\n";
         }
      }
      $headers = array('Content-Type: text/plain; charset="UTF-8";',
       'From: ' . $from,
       'Reply-To: ' . $from,
       'Return-Path: ' . $from,
      );
      mail($sendTo, $subject, $emailText, implode("\n", $headers));
      $responseArray = array('type' =&gt; 'success', 'message' =&gt; $okMessage);
      }
  }
  catch (\Exception $e) {
    $responseArray = array('type' =&gt; 'danger', 'message' =&gt; $e-&gt;getMessage());
  }
  if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
   $encoded = json_encode($responseArray);
   header('Content-Type: application/json');
   echo $encoded;
  } else {
  echo $responseArray['message'];
  }
  </code></pre>
  
  <p><strong>--- JS AND AJAX CODE ---</strong></p>
  
  <pre><code>$(function () {
    $('#contact-form').on('submit', function (e) {
     if (!e.isDefaultPrevented()) {
      var url = "contact.php";
  
      $.ajax({
       type: "POST",
       url: url,
       data: $(this).serialize(),
       success: function (data) {
        var messageAlert = 'alert-' + data.type;
        var messageText = data.message;
        var alertBox = '&lt;div class="alert ' + messageAlert + ' alert-dismissable"&gt;&lt;button type="button" class="close" data-dismiss="alert" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;' + messageText + '&lt;/div&gt;';
        if (messageAlert &amp;&amp; messageText) {
         $('#contact-form').find('.messages').html(alertBox);
         $('#contact-form')[0].reset();
        }
       }
      });
      return false;
  
     }
    })
  });
  </code></pre>