我在其中创建了一个带有输入的表单,我试图通过ajax调用发送到php表单以允许我收到电子邮件。
从我的网络标签看,我没有看到我的php文件email-project.php
出现,除非我在错误的位置查找。我不确定我做错了什么。我得到的唯一错误是我的ajax变量中此行的意外标记:
var project-name = $("#project-name").val();
有人看到了什么问题吗?
<form action="" autocomplete="on" method="POST" id="project-information-form">
<input type="text" class="input-borderless" id="project-name" placeholder="Your Name">
<input type="text" class="input-borderless" id="title-roll" placeholder="Title/Role">
<input type="email" class="input-borderless" id="project-email" placeholder="Email Address">
<input type="text" class="input-borderless" id="project-number" placeholder="Phone Number">
<input type="text" class="input-borderless" id="project-company" placeholder="Company/URL">
</div>
<div class="project-input-container2">
<textarea rows="3" class="input-borderless" id="project-description" placeholder="Describe the project"></textarea>
<input type="text" class="input-borderless" id="project-source" placeholder="How did you hear about us?">
<input type="text" class="input-borderless" id="project-socialMedia" placeholder="Which of our social media influenced you the most?">
<input type="text" class="input-borderless" id="project-humanTest" placeholder="Human Test: What day comess after Thursday?">
</div>
<input type="submit" id="submit-project" value="Send Project Inquiry">
</form>
AJAX
$(document).ready(function(){
$("#submit-project").on("click", function () {
var project_name = $("#project-name").val();
var title_roll = $("#title-roll").val();
var project_email = $("#project-email").val();
var project_number = $("#project-number").val();
var project_company = $("#project-company").val();
var project_description = $("#project-description").val();
var project_source = $("#project-source").val();
var project_socialMedia = $("#project-socialMedia").val();
var project_humanTest = $("#project-humanTest").val();
$.ajax({
url: "email-project.php",
type: "POST",
data: {
"project_name": project_name,
"title_roll": title_roll,
"project_email": project_email,
"project_number": project_number,
"project_description": project_description,
"project_source": project_source,
"project_socialMedia": project_socialMedia,
"project_humanTest": project_humanTest
},
success: function (data) {
// console.log(data); // data object will return the response when status code is 200
if (data == "Error!") {
alert("Unable to send email!");
alert(data);
} else {
$(".announcement_success").fadeIn();
$(".announcement_success").show();
$('.announcement_success').html('Announcement Successfully Added!');
$('.announcement_success').delay(5000).fadeOut(400);
}
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + "|" + errorThrown);
//console.log("error"); //otherwise error if status code is other than 200.
}
});
});
});
PHP
ini_set('display_errors', 1);
error_reporting(E_ALL);
$project_name = $_POST['project_name'];
$title_roll = $_POST['title_roll'];
$project_email = $_POST['project_email'];
$project_number = $_POST['project_number'];
$project_description = $_POST['project_description'];
$project_source = $_POST['project_source'];
$project_socialMedia = $_POST['project_socialMedia'];
$project_humanTest = $_POST['project_humanTest'];
$to = 'email';
$subject = 'Project Inquiry Form Sent';
$message = 'FROM: '.$project_name. "<br>" . ' Email: '.$project_email. "<br>" . 'Message: '.$project_description;
$headers = 'From:' .$project_email . "\r\n";
if (!empty($email)) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//Should also do a check on the mail function
if (mail($to, $subject, $message, $headers)) {
echo "Your email was sent!"; // success message
} else {
echo "Mail could not be sent!"; // failed message
}
} else {
//Invalid email
echo "Invalid Email, please provide a valid email address.";
}
} else {
echo "Email Address was not filled out.";
}