Send email without page refresh PHP AJAX. The email.php sends email action is in HTML

I want to send it with AJAX. Where am I going wrong with this? The error I get is:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.send @ jquery.min.js:4

$(document).ready(function() {
  $("#submit-email").click(function(event) {
    var email = document.getElementById('mail').value;
    var message = document.getElementById('message').value;
    var dataString = {
      "mail": email,
      "message": message
    }

    $.ajax({
      type: "post",
      url: "controller/email.php",
      data: dataString,
      success: function(html) {
        alert('Success!');
      }
    });
    event.preventDefault();
  });
});
<form>
  <input type="email" name="mail" id="mail" required="required" class="form" placeholder="Email" />
  <input type="hidden" name="message" id="message" value="test value" />
  <button type="submit" id="submit-email" name="submit" class="submit-email">Send Message</button>
</form>
<?php

if(isset($_POST['submit']) ){ 

$youremail = '[email protected]';   
$user_email = $_POST['mail'];
$message = $_POST['message'];

$and = 'blahblah.net';
$body = "You requested we email the following to you: $message. Regards, Team";

$headers = "From: $youremail"; 

mail($user_email, 'blahblah.net', $body, $headers ); 

}

$newURL = '../index-open.php';
header('Location: '.$newURL);


?>

Upvotes: 0

Views: 46

Answers (1)

Zeeshan Eqbal
Zeeshan Eqbal

Reputation: 260

Try this:

It may help.

$(document).ready(function() {
  $("form").submit(function(e) {
    e.preventDefault();
    var email = document.getElementById('mail').value;
    var message = document.getElementById('message').value;
    var dataString = {
      "mail": email,
      "message": message
    }

    $.ajax({
      type: "post",
      url: "controller/email.php",
      data: dataString,
      success: function(html) {
        alert('Success!');
      }
    });

  });
});

Upvotes: 0

Related Questions