$(document).ready(function() {
$('#contact-form').submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
// Serialize the form data.
var formData = $(this).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: formData
})
.done(function(response) {
// Display a success message.
alert('Спасибо за обращение. Мы свяжемся с вами в ближайшее время!');
})
.fail(function(xhr, status, error) {
// Display an error message.
alert('Возникла ошибка. Пожалуйста, попробуйте еще раз.')
});
});
});
PHP (sendmail.php):
code
<?php
// Get the form data.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Set the recipient email address.
$to = 'youremail@example.com';
// Set the email subject.
$subject = 'Сообщение с сайта';
// Set the email message.
$message = 'Имя: ' . $name . "\n"
. 'Email Адрес: ' . $email . "\n"
. 'Сообщение: ' . $message;
// Set the email headers.
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n"
. 'Reply-To: ' . $email . "\r\n"
. 'X-Mailer: PHP/' . phpversion();
// Send the email.
if (mail($to, $subject, $message, $headers)) {
echo 'success';
} else {
echo 'error';
}
?>
Но обратите внимание, что этот пример кода имеет базовую функциональность и не подходит для продакшн сайтов. Вам нужно будет настроить дополнительную валидацию и защиту от вредоносных атак.