remy boys remy boys - 12 days ago 11
Ajax Question

ajax form getting submitted but no response

so i'm submitting this form from a ajax request and its working fine only thing is that there's no response callback after submission

here's my codes :

//index.html
<form method="POST" id="myForm" action="sendmail.php">
<input type="text" name="sender_email" placeholder="Email" required="">
<textarea placeholder="Message" name="message" required=""></textarea>
<input type="submit" name="send" value="SEND">
</form>


$(document).ready(function() {
$("#myForm").on('submit', function(event) {
event.preventDefault();
var formData = $(this).serialize();
$.ajax({
type: 'POST',
url: 'sendmail.php',
dataType: "json",
data: formData,
success: function(response) {
alert("Mail sent"); // no callback here
},
error: function(xhr, status, error) {
console.log(xhr);
}
});
});
});


my php:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['message'])){
$to = 'support@mydomain.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: ".$_POST['sender_nam‌​e​']." <".$_POST['sender_em‌​ail‌​'].">\r\n"; $headers = "Reply-To: ".$_POST['sender_ema‌​il‌​']."\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo json_encode(["success" => true]);
} else {echo json_encode(['success'=>false]);
}
exit;
}
?>


anyone can point out whats wrong here ??

Answer

You are referring to $_POST['subject'], $_POST['sender_nam‌​e​'], $_POST['sender_nam‌​e​'] in your sendmail.php file but not sending these by your form. That's why php throws warnings and you'r not getting any valid ajax response.