Witam
Mam problem z ajaxem gdy dodam opcję dataType: 'json', to wtedy kod przestaje działać i wyświetla się funkcja error, próbowałem to jakoś naprawić ale nie mam już pomysłów, jakby ktoś pytał to robię to na xampp ,tutaj mój kod:
$.ajax({
type: 'POST',
url: 'sendmail.php',
dataType: 'json',
data: {
firstlastname: FirstName,
email: Email,
phonenumber: PhoneNumber,
message: Message
},
success: function (data) {
$('#sendmailRESULT').html('Twoja wiadomość została wysłana, postaramy sie odpowiedzić w ciągu 24!');
$('#sendmailRESULT').addClass('SendMailTRUE');
},
error: function () {
$('#sendmailRESULT').html('Przepraszamy ale nie udało się wysłać twojej wiadomości mamy chwilowe problemy z serwerem, zapraszamy ponownie później!');
$('#sendmailRESULT').addClass('SendMailFALSE');
}
});
<?php
header("Content-type: application/json");
if (!empty($_POST['firstlastname'] && $_POST['email'] && $_POST['phonenumber'] && $_POST['message'])) {
$FirstLastName = $_POST['firstlastname'];
$Email = $_POST['email'];
$PhoneNumber = $_POST['phonenumber'];
$MessagePOST = $_POST['message'];
$To= 'abc@wp.pl';
$Title = 'Title';
$Message = '';
$Message .= 'Imię I Nazwisko: '.$FirstLastName.'\n';
$Message .= 'Email: '.$Email.' Numer Telefonu: '.$PhoneNumber.'\n';
$Message .= 'Wiadomość:'.$MessagePOST.'\n';
$Headers = 'From: a@wp.pl'.'\r\n';
$SendMail = mail($To, $Title, $Message, $Headers);
$data = array();
if ($SendMail) {
$data['SendMailTRUE'] = 'true';
echo json_encode($data);
} else {
$data['SendMailFALSE'] = 'false';
echo json_encode($data);
}
} else {
header('Location: index.php');
}
?>