I have a simple contact form

```
<form role="form" id="contactForm" method="post" action="php/mailer.php">
    <div class="form-group">
        <input type="text" class="form-control form-effect" name="name" id="name" placeholder="Name" required>
    </div>
    <div class="form-group">
        <input type="email" class="form-control form-effect" name="email" id="email" placeholder="Email" required>
    </div>
    <div class="form-group">
        <textarea type="textarea" class="form-control form-effect" name="message" id="message" placeholder="Message"></textarea>
    </div>
    <button type="submit" class="btn btn-default btn-sub" id="sbtBtn">Submit</button>
</form>
<div id="form-messages"></div>

```

When the form is submitted, my javascript file acts on its submit

```
var form = $('#contactForm');
var formMessages = $('#form-messages');

$(form).submit(function(event) {
    event.preventDefault();
    var formData = $(form).serialize();

    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData,
        dataType: "json"
    }).done(function(response) {
        $(formMessages).text(response);
        $(form)[0].reset();
    }).fail(function(data) {
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! An error occured and your message could not be sent.');
        }
    });
});

```

This then calls my php file mailer.php

```
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $to_email = "test@gmail.com";
    $name      = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
    $email     = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $message   = filter_var($_POST["message"], FILTER_SANITIZE_STRING);

    if(strlen($name)<2){ // If length is less than 2 it will output JSON error.
        json_encode('Name is too short or empty!');
    }
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //email validation
        json_encode('Please enter a valid email!');
    }

    $message_body = $message."\r\n\r\n-".$name."\r\nEmail : ".$email."\r\n (".$message.") " ;

    $headers = 'From: '.$name.'' . "\r\n" .
        'Reply-To: '.$email.'' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    $send_mail = mail($to_email, "Contact from website", $message_body, $headers);

    if(!$send_mail) {
        json_encode('Could not send mail! Please try again.');
    }else{
        json_encode("Thank you for your message, we will contact you shortly");
    }
}

```

For some reason, the output I get displayed is null, and no email is sent. I have been trying to figure out why but can't seem to find anything. I know it gets into the PHP file, but not sure what problem it is having.

I dont even know where it is getting the null output from?