我可能会在此处获得重复,但我已经搜索过并搜索过,我找不到遇到相同问题的人使用相同的语法,所以希望有人可以看到我发现的事情。我失踪了。 PHP不是我的强项,但我已经完成了几个教程和课程,并做了一些努力改进的工作,这就是为什么这让我烦恼不已。我有一个非常简单的html表单,我试图使用PHP发送到电子邮件。我发现了一种不同的方式从我习惯使用的方式来执行此操作,并希望尝试一下。当函数触发时,一切似乎都在工作(我已经使用console.log,alert进行了测试,弹出窗口确实弹出,所以"成功"正在PHP文件中工作我没有弄错。由此产生的问题是我从未收到表格中的电子邮件(为了提出这个问题,我发了一封虚拟电子邮件)。
我知道我没有表格行动=" something.php"或直接从表单中调用函数,但从我从这种方法中理解的是,一切都在提交时触发。同样,由于一切似乎都在运作,但电子邮件,我不确定这笔交易是什么。
我很想看看更有经验的眼睛能否找到我所缺少的东西。如果重要,主机提供商是Godaddy。非常感谢任何人提供的帮助。
这是我的html(页面是.php扩展名):
<form class="main-contact-form" id="main-contact-form">
<div class="row">
<div class="col-md-6">
<input id="first-name" name="firstName" type="text" placeholder="First Name (Required)" onfocus="this.placeholder = ''" onblur="this.placeholder = 'First Name (Required)'" />
</div>
<div class="col-md-6">
<input id="last-name" name="lastName" type="text" placeholder="Last Name (Required)" onfocus="this.placeholder= ''" onblur="this.placeholder= 'Last Name (Required)'" />
</div>
<div class="col-md-6">
<input id="email" name="email" type="email" placeholder="Email (Required)" onfocus="this.placeholder= ''" onblur= "this.placeholder= 'Email (Required)'" />
</div>
<div class="col-md-6">
<input id="phone" name="phone" type="phone" placeholder= "Phone Number" onfocus= "this.placeholder= ''" onblur="this.placeholder= 'Phone Number'" />
</div>
<div class="col-md-6">
<input id="company" name="company" type="text" placeholder="Company" onfocus="this.placeholder= ''" onblur= "this.placeholder= 'Company'" />
</div>
<div class="col-md-6">
<select id="contact-type" name="contactType">
<option value="" selected>I'd like to...</option>
<option class="contact-option" value="Contact sales">Contact sales</option>
<option value="Get a quote">Get a quote</option>
<option value="Contact customer service">Contact customer service</option>
</select>
</div>
<div class="col-12">
<textarea id="message" placeholder="What can we do for you?" onfocus="this.placeholder= ''" onblur="this.placeholder= 'What can we do for you?'" /></textarea>
</div>
<div class="col-md-6 push-md-3">
<button type="submit">Submit</button>
</div>
</div>
</form>
这是我的js代码:
var contactForm = $("#main-contact-form");
contactForm.submit(function(event) {
event.preventDefault();
submitForm();
});
function submitForm() {
var firstName = $("#first-name");
var lastName = $("#last-name");
var email = $("#email");
var phone = $("#phone");
var company = $("#company");
var contactType = $("#contact-type");
var message = $("#message");
$.ajax({
type: "POST",
url: "php/main-contact-form.php",
data: "firstName=" + firstName + "&lastName" + lastName + "&email" + email + "&phone" + phone + "&company" + company + "&contactType" + contactType + "&message" + message,
success: function(text) {
if (text == "success"){
formSuccess();
}
}
});
}
var contactSuccess = $(".contact-submission-success")
function formSuccess(){
contactSuccess.addClass("show", 200);
}
});
这是我的PHP代码:
<?php
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$company = $_POST["company"];
$contactType = $_POST["contactType"];
$message = $_POST["message"];
$EmailTo = "fakeemail@fakeemail.com";
$subject = "New Contact Form Message Received";
$Body .= "First Name: ";
$Body .= $firstName;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $lastName;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $company;
$Body .= "\n";
$Body .= "Contact Type: ";
$Body .= $contactType;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
if ($success) {
echo "success";
} else {
echo "invalid";
}
?>
答案 0 :(得分:5)
好吧,我认为你的问题就在这里:
function submitForm() {
var firstName = $("#first-name");
var lastName = $("#last-name");
var email = $("#email");
var phone = $("#phone");
var company = $("#company");
var contactType = $("#contact-type");
var message = $("#message");
$.ajax({
type: "POST",
url: "php/main-contact-form.php",
data: "firstName=" + firstName + "&lastName" + lastName + "&email" + email + "&phone" + phone + "&company" + company + "&contactType" + contactType + "&message" + message,
success: function(text) {
if (text == "success"){
formSuccess();
}
}
});
}
您需要获取输入的值,而不是输入本身。例如,如果您这样做:
var miValue = $("#first-name");
你得到的是dom元素,而不是值。要获取该值,必须调用val函数:
var miValue = $("#first-name").val();
你的功能应如下所示:
function submitForm() {
var firstName = $("#first-name").val(); //Call the val() function
var lastName = $("#last-name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var company = $("#company").val();
var contactType = $("#contact-type").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "php/main-contact-form.php",
data: "firstName=" + firstName + "&lastName" + lastName + "&email" + email + "&phone" + phone + "&company" + company + "&contactType" + contactType + "&message" + message,
success: function(text) {
if (text == "success"){
formSuccess();
}
}
});
}
您可以在此处详细了解val:val() jQuery
答案 1 :(得分:0)
我认为问题出在您的电子邮件中。这是一个有效的吗?您不需要发布它,只需在PHP代码中硬编码或使用配置值更好。