title: Why does AJAX validation result in showing up PHP error message?
body: <p>I am trying to create a contact form which 1) sends a formatted message to the recipient e-mail, and 2) shows a success/error pop up message on the same page after the user submits the form.</p>
  <p>I've tried to use JQuery AJAX form validation, but that causes my website to behave awkwardly (it already runs on Bootstrap CDN). This is why I use vanilla JavaScript for the AJAX request.</p>
  <p>Currently, when I want to visit the contactform.html page, I immediately get a white screen with just an object with the default PHP message and status. What causes this?</p>
  <p><strong>AJAX request</strong></p>
  <pre><code>const contactForm = document.addEventListener('DOMContentLoaded', function() {
              document.getElementById('contact-form').submit(function(e) {
                  e.preventDefault();
  
                  const xhr = new XMLHttpRequest();
                  const url = 'contactform3.php';
                  xhr.open('POST', url, true);
                  xhr.setRequestHeader('Content-type', 'application/json');
                  xhr.onreadystatechange = function() {
                      if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {
                          let jsonData = JSON.parse(xhr.response);
                          console.log(jsonData);
                          document.getElementById('contact-form').reset();
                          document.querySelector('.response').innerHTML = 'Bedankt voor uw bericht. Wij nemen zo snel mogelijk contact met u op.';
                      } else {
                          document.querySelector('.response').innerHTML = 'Er is iets misgegaan.';
                      }
                  };
              })
          })
  </code></pre>
  <p><strong>PHP</strong></p>
  <pre><code>&lt;?php
      // Recipient Email
      $toEmail = 'info@example.com';
  
      $status = 0;
      $statusMsg = 'Er is iets misgegaan.';
  
      if(isset($_POST['contact_submit'])){
  
          // Get the submitted data
          $assignmentchoice = $_POST['assignmentchoice'];
          $projectchoice = $_POST['projectchoice'];
          $name = $_POST['name'];
          $lastname = $_POST['lastname'];
          $email = $_POST['email'];
          $tel = $_POST['tel'];
          $address = $_POST['address'];
          $zip = $_POST['zip'];
          $message = $_POST['message'];
  
          if(!empty($assignmentchoice) &amp;&amp; !empty($projectchoice) &amp;&amp; !empty($name) &amp;&amp; !empty($lastname) &amp;&amp; !empty($email) &amp;&amp; !empty($message)) {
              if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                  $statusMsg = 'Vul een geldig e-mail adres in.';
              } else {
                  $emailSubject = 'U heeft een nieuw bericht van'.$name;
                  $htmlContent = '&lt;h2&gt;Je hebt een nieuw bericht&lt;/h2&gt;
                      &lt;h4&gt;Opdracht:&lt;/h4&gt;&lt;p&gt;'.$assignmentchoice.'&lt;/p&gt;
                      &lt;h4&gt;Project:&lt;/h4&gt;&lt;p&gt;'.$projectchoice.'&lt;/p&gt;
                      &lt;h4&gt;Naam:&lt;/h4&gt;&lt;p&gt;'.$name.'&lt;/p&gt;
                      &lt;h4&gt;Achternaam:&lt;/h4&gt;&lt;p&gt;'.$lastname.'&lt;/p&gt;
                      &lt;h4&gt;Email adres:&lt;/h4&gt;&lt;p&gt;'.$email.'&lt;/p&gt;
                      &lt;h4&gt;Telefoonnummer:&lt;/h4&gt;&lt;p&gt;'.$tel.'&lt;/p&gt;
                      &lt;h4&gt;Adres locatie:&lt;/h4&gt;&lt;p&gt;'.$address.'&lt;/p&gt;
                      &lt;h4&gt;Postcode locatie:&lt;/h4&gt;&lt;p&gt;'.$zip.'&lt;/p&gt;
                      &lt;h4&gt;Bericht:&lt;/h4&gt;&lt;p&gt;'.$message.'&lt;/p&gt;';
  
                  $headers = &quot;MIME-Version: 1.0&quot; . &quot;\r\n&quot;;
                  $headers .= &quot;Content-type:text/html;charset=UTF-8&quot; . &quot;\r\n&quot;;
                  $headers .= 'From: '.$name.'&lt;'.$email.'&gt;'. &quot;\r\n&quot;;
  
                  $sendEmail = mail($toEmail, $emailSubject, $htmlContent, $headers);
                  if($sendEmail) {
                      $status = 1;
                      $statusMsg = 'Bedankt voor uw bericht. Wij nemen zo snel mogelijk contact met u op.';
                  } else {
                      $statusMsg = 'Er is iets misgegaan.';
                  }
              }
          } else {
              $statusMsg = 'Vul alstublieft alle velden in.';
          }
      }
  
      $response = array(
          'status' =&gt; $status,
          'message' =&gt; $statusMsg
      );
      echo json_encode($response);
  ?&gt;
  </code></pre>