This is my absolute first try on the subject: I'm sending data throw email using javascript and PHP. I have read previous responses on the subject, but I can't make it work. My js code:
const messagesend = document.getElementById('message_form');
messagesend.addEventListener('submit', (eee) => {
eee.preventDefault();
var messageData = new Object();
messageData.name = document.getElementById('conname').value;
messageData.email = document.getElementById('conemail').value;
messageData.message = document.getElementById('conmessage').value;
messageData.verification = 'drovarcrete_message';
var messageString = JSON.stringify(messageData);
$.ajax({
type: "POST",
contentType: 'application/json',
url: 'messageSender.php',
data: messageString,
dataType: 'json',
success: function(data) {
alert("Message send.");
$('#contactModal').modal('hide');
$('#email_success_info').modal('show');
}
});
});
$("#modal2_close").click(function() {
$('#email_success_info').modal('hide');
});
My PHP works fine, with last line being: echo 'Message send'; I have also tried: echo json_encode(array('success' => true));
However, the alert message doesn't show.
Use the following and see what is going on with your request:
$.ajax({
type: "POST",
contentType: 'application/json',
url: 'messageSender.php',
data: messageString,
dataType: 'json',
success: function(data) {
alert("Message send.");
$('#contactModal').modal('hide');
$('#email_success_info').modal('show');
},
error: function (error) {
console.log(error.responseText);
}
});