I'm trying to try a pretty simple request and get response thing using $.ajax via. json. I have tried alot to return a simple text phrase from a php file. My js part of code goes like this that calls a beemailer.php to get response.
$.ajax({
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
url: 'beemailer.php',
data: {
'name': data_name,
'email': data_email,
'message': data_message,
'subject': data_subject
},
success: function (data) {
var output = jQuery.parseJSON(data);
console.log(output);
alert(output);
},
error: function (error, x, y) {
console.log(error);
alert(x, y);
}
)
};
And beemailer.php is like this:
<?php
$to = "[email protected]";
$subject = $_POST['subject'];
$message =$_POST['message'];
$headers = "From:".$_POST['name']."(".$_POST['email'].")";
mail($to,$subject,$message,$headers);
$response = "The mail has been sent. Thank You the valid mail.";
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
?>
What I need for now is simply to get that $response text get alerted. Any help will be appreciated.