remy boys remy boys - 1 year ago 99
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 :

<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">

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

my php:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$to = '';
$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]);

anyone can point out whats wrong here ??

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download