PHP form sending email with Ajax blocking


Hello, I’m starting with PHP, and I was working on a personnal project using AJAX.

Here is my simple  HTML form with 3 fields and a button, I was hopping sending email and giving the user the feedback without refreshing the page:


<!– Contact Form –>
<form action=”” method=”post” id=”ContactFrm”>
           <div class=”form-group”>
                  <input id=”name” type=”text” class=”form-control-input” placeholder=”Name” name=”Name” required>
           <div class=”form-group”>
                   <input id=”email” type=”email” class=”form-control-input” placeholder=”Email” name=”Email” required>
                   <div class=”form-group”>
             <textarea id=”message” class=”form-control-textarea” placeholder=”Message” name=”Message” required></textarea>
              <div class=”form-group”>
                     <button id=”submitBtn” type=”button” class=”form-control-submit-button” name=”submit” value=”submit”>Submit</button>
    <center><div id=”feedback” class=”form-control-input”></div></center>


Here is the script in the head section:

<script src=””></script>

        $(document).ready(function () {
            $(‘.submitBtn’).click(function (e) {
                        type: “POST”,
                        url: “mail_handler.php”,
                        data: $(‘ContactFrm’).serialize(),
                        success: function (html) {
                            alert(‘form was submitted’);


And finally this is my mail_handler.php using PHPMailer wich works fine until I added the Ajax part calling the PHP:


error_reporting(E_ALL); ini_set(‘display_errors’, 1);

#load phpmailer
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
use PHPMailerPHPMailerSMTP;

require ‘vendor/Exception.php’;
require ‘vendor/PHPMailer.php’;
require ‘vendor/SMTP.php’;

#Receive user input
 $name = $_POST[‘name’];
 $email = $_POST[’email’];
 $message = $_POST[‘message’]; 

$mail = new PHPMailer();
$mail->SMTPDebug = 1;

$mail->Host = ‘’;
$mail->Port = 587;
$mail->SMTPSecure = ‘tls’; 
$mail->SMTPAuth = false; 
$mail->From = $email;               
$mail->AddAddress(‘[email protected]’);
$mail->Subject    =  “Hello”;                      
$mail->Body = $message;                 

if (!$mail->send()) 
      echo $mail->ErrorInfo;
      echo”<p>The mail() function failed.</p>”;
      echo”<p>Email Sent !!!</p>n”;


For some unknown reasons the call do not transfer fileds values to php, giving the error : PHP Notice:  Undefined index: for name, email and message.

I’ve been working for a while on this bug and tried a lot of things but can’t get the message in the feedback section of the form.

Thanks in advance for nay help.