привет просто нужно отправить сообщение на gmail. Отправка формы должна быть реализована с использованием jQuery.
я думаю, что не могу передать данные из jquery в php.
без файла jquery сообщение отправляется на gmail, но когда я включаю jquery на html, он говорит, что сообщение было успешно отправлено, но ничего не появляется на gmail
это основной файл PHP
<?php
require 'Sendmail.php';
?>
<!doctype html>
<html lang = "en">
<head>
<title>Contact Us</title>
</head>
<body>
<section>
<h1>Contact Form</h1>
<form action = "" method = "POST">
<label>Firstname</label>
<input type = "text" name = "firstname" id = "firstname">
<label>Lastname</label>
<input type = "text" name = "lastname" id = "lastname">
<label>Email</label>
<input type = "text" name = "email" id = "email">
<label>Subject</label>
<input type = "text" name = "subject" id = "subject">
<label>message</label>
<textarea name = "message" id = "message"></textarea>
<a href = "index.php">Sign Up</a>
<button type = "submit" id = "button" name = "submit" value = "Send message">Send message</button>
</form>
</section>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script type = "text/javascript" src = "ContactValidation.js"></script>
</body>
</html>
это файл JQUERY
$(function(){
$('#button').click(function(e){
var valid = this.form.checkValidity();
if (valid){
var firstname = $('#firstname').val();
var lastname = $('#lastname').val();
var email = $('#email').val();
var subject = $('#subject').val();
var message = $('#message').val();
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
e.preventDefault();
if (firstname == "" || lastname == "" || email== "" || subject == "" || message == ""){
Swal.fire({
'title': 'Errors',
'text': 'All fields are compulsory',
'type': 'error'
})
}
else {
if (firstname.length>=64 || firstname.match(/[^\w\s]/gi)) {
Swal.fire({
'title': 'error',
'text': 'Please enter a valid firstname',
'type': 'error'
})
}
else if (lastname.length>=64 || lastname.match(/[^\w\s]/gi)) {
Swal.fire({
'title': 'error',
'text': 'Please enter a valid lastname',
'type': 'error'
})
}
else if (!email.match(regex)){
Swal.fire({
'title': 'error',
'text': 'Please enter a valid email',
'type': 'error'
})
}
else if (subject.length>=64){
Swal.fire({
'title': 'error',
'text': 'Subject must contain less than 64 character',
'type': 'error'
})
}
else {
$.ajax({
type: 'POST',
url: 'Sendmail.php',
data: {firstname: firstname,lastname: lastname,email: email, subject:subject, message:message},
success: function(data){
Swal.fire({
'title': 'Successful',
'text': data,
'type': 'success'
})
}
});
}
}
}
});
});
и это файл PHP, который отправляет сообщение на gmail
<?php
error_reporting(0);
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$sub = $_POST['subject'];
$message = $_POST['message'];
if (isset($_POST['submit'])){
$to = '[email protected]';
$subject = "Subject: " . $sub ;
$subject2 = "Copy of your subject: " . $sub;
$message = $firstname . $lastname . " wrote the following message: \n\n" . $message;
$message2 = "Copy of the message: " . $message;
$headers = "From: " .$email;
$headers2= "From: " . $to;
mail($to,$subject,$message,$headers);
mail($email,$subject2,$message2,$headers2);
}
echo "Email sent! Thank you " . $firstname . ", We will contact you shortly.";



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Как уже упоминалось @BlackXero, вы выполняете следующую проверку:
if (isset($_POST['submit']))
Обычно это используется, когда вы отправляете форму непосредственно из html или php в Sendmail.php. Так что в этом случае вам не нужна эта проверка.
Также в этом примере ваша форма все еще пытается отправить куда-то из-за того, что вы добавили атрибуты метода и действия для своей формы. В этом случае вы могли бы (вероятно, должны) удалить их, а также добавить e.preventDefault() в начало функции нажатия кнопки.
Вам не нужно включать это требование «Sendmail.php», так как форма отправляется с использованием ajax. и напишите свое сообщение, т.е. «отправить по электронной почте», если условие
ваше действие содержит условие if (isset($_POST['submit'])){ и в вашем запросе ajax вы его не отправляете, просто измените объект на {firstname: firstname,lastname: lastname,email: email, subject: тема, сообщение: сообщение, "отправить": "отправить"} и, надеюсь, это сработает