您好,只需要在gmail上发送邮件即可。 表单提交应使用jQuery来实现。
我认为我无法将数据从jquery传递到php。
没有在gmail上发送jquery文件消息,但是当我在html上包含jquery时,它表示消息已成功发送,但在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'
})
}
});
}
}
}
});
});
这是在gmail上发送消息的PHP文件
<?php
error_reporting(0);
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$sub = $_POST['subject'];
$message = $_POST['message'];
if (isset($_POST['submit'])){
$to = 'g_chkuaseli@cu.edu.ge';
$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.";
答案 0 :(得分:0)
如@BlackXero所述,您正在执行以下检查:
if (isset($_POST['submit']))
通常在您直接从html或php向Sendmail.php提交表单时使用。因此,在这种情况下,您不需要该支票。
在此示例中,由于您为表单添加了方法和操作属性,因此您的表单仍在尝试在某处提交。在这种情况下,您可以(可能应该)删除这些内容,并将e.preventDefault()
添加到按钮单击功能的开头。
答案 1 :(得分:0)
由于表单是使用Ajax提交的,因此您不需要包含此要求“ Sendmail.php”。 并向您发送消息电子邮件,如果条件满足