title: Why my echo json_encode($data) actually displays something on my pages and how to avoid that ( ajax/php form validation )
body: <p>EDIT : I resolved my issue as I had a forgotten "require" operator messing up in the code. Sorry for not being aware of this earlier and thanks for your time.</p>
  
  <p>I'm kinda new to web developpement and it's the second time only I use AJAX on a website.</p>
  
  <p>It's just about a contact form validation using ajax and php client side validation.
  Here is the problem : my code is actually working as expected for the validation part as everything works fine but I don't understand what I'm missing or doing wrong cause I get a line on top of my website (on each page, not only the contact page btw ) that I don't want obviously</p>
  
  <p>I understand that it comes from my php code actually echoing my json_encode($data) but i don't get why and how to do to avoid this</p>
  
  <p>Here is my code :</p>
  
  <p>JS :</p>
  
  <pre><code>        let resultMessage = $('#resultMessage')
          let form = $('#contact_form')
  
          form.submit(function(e){
              $('span.error').empty()
              e.preventDefault()
  
              $.ajax({
  
  
                  type: "POST",
                  url: form.attr("action"),
                  data: form.serialize(),
                  dataType: 'json',
  
                  success: function(response) {
                      if(response.success == true){
                          resultMessage.html('Message sent succesfully')
                      } else {
                          resultMessage.html('Fail')
                          if (response.errors.nom != null) {
                              $('#name_error').html(response.errors.nom);
                          }
                          if (response.errors.email != null) {
                              $('#email_error').html(response.errors.email);
                          }
                          if (response.errors.message != null) {
                              $('#message_error').html(response.errors.message);
                          }
                      }
                  },
                  error: function(){
                      console.log('error')
                  }
              })
          })
  </code></pre>
  
  <p>PHP </p>
  
  <pre><code>$success = false;
  $errors = [];
  
  
      // inputs validation
      $errors = textInputValidation($errors, $name, 'name', 2, 50);
      $errors = emailValidation($errors, $email, 'email');
      $errors = textInputValidation($errors, $message, 'message', 10, 5000);
  
      // if there is no error =&gt;
      if (count($errors) == 0) {
  
          // code actualy sending the message 
  
          $success = true;
      } 
  
      $data = array(
          'errors' =&gt; $errors,
          'success' =&gt; $success
      );
  
      header('Content-type:application/json');
      echo json_encode($data);
  </code></pre>
  
  <p>So this code is working but the problem is that I get this line on top of my website</p>
  
  <pre><code>{"errors":{"name":"please fill this field","email":"please fill this field","message":"please fill this field"},"success":false}
  </code></pre>
  
  <p>It's on each page, not only the contact page, and it's not even changing when the validation is okay and there is no errors in the form or anywhere.</p>
  
  <p>I'm probably missing something very basic, sorry for that.</p>
  
  <p>Thanks for your help</p>