我不确定我做错了什么,我已经联系我们发送电子邮件的表格。但是点击“提交”后没有任何反应。我正在使用Materialise CSS for UI。
<div id="modal-contact" class="modal">
<div class="modal-content">
<h5 class="center">Get in touch</h5>
<div class="row">
<form id="contact" method="post" class="col s12">
<div class="row">
<div class="input-field col s12 m12 l4">
<input id="first_name" type="text" class="validate">
<label for="first_name">First Name</label>
</div>
<div class="input-field col s12 m12 l4">
<input id="last_name" type="text" class="validate">
<label for="last_name">Last Name</label>
</div>
<div class="input-field col s12 m12 l4">
<input id="phone" type="tel" class="validate" maxlength="10" minlength="10">
<label for="phone">Phone</label>
</div>
<div class="input-field col s12">
<input id="email" class="validate" type="email">
<label for="email">Email Address</label>
</div>
<div class="input-field col s12">
<textarea id="message" class="materialize-textarea validate"></textarea>
<label for="message">How can we help?</label>
</div>
</div>
</form>
<div class="hide" id="msgSubmit"></div>
</div>
<div class="modal-footer">
<button class="btn waves-effect light-green accent-4" type="submit">Submit <i class="material-icons right">send</i></button>
</div>
</div>
我也在使用Ajax,如下所示:
$(function (){
$("#contact").on("submit", function(e){
e.preventDefalut();
submitForm();
});
});
function submitForm() {
var fname = $("#first_name").val();
var lname = $("#last_name").val();
var phone = $("#phone").val();
var email = $("#email").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "contact.php",
data: "fname=" + fname + "&lname=" + lname + "&phone=" + phone + "&email=" + email + "&message=" + message,
sucess : function(text) {
if(text == "success") {
formSuccess();
} else {
formError();
submitMSG(false, text);
}
}
});
};
function formSuccess() {
$("#contact")[0].reset();
submitMSG(true, "We have received your message and would like to thank you for writing to us. If your inquiry is urgent, please use the telephone number to contact us")
}
function formError() {
$("#contact").removeClass().addClass(), function() {
$(this).removeClass();
}
}
function submitMSG(valis, MSG) {
if(valid) {
var msgClasses = "h3";
} else {
var msgClasses = "h3";
}
$("#msgSubmit").removeClass.addClass(msgClasses).text(msg);
}
最后是PHP代码
<?php
$errroMSG = "";
if (empty($_POST["fname"])) {
$errorMSG = "First Name is required ";
} else {
$fname = $_POST["fname"];
}
if (empty($_POST["lname"])) {
$errorMSG = "Last Name is required ";
} else {
$lname = $_POST["lname"];
}
if (empty($_POST["phone"])) {
$errorMSG = "Phone is required ";
} else {
$phone = $_POST["phone"];
}
if (empty($_POST["email"])) {
$errorMSG = "Email is required ";
} else {
$email = $_POST["email"];
}
if (empty($_POST["message"])) {
$errorMSG = "Message is required ";
} else {
$pmessage = $_POST["message"];
}
$EmailTo = "test@gmail.com";
$Subject = "New message from United Constructions";
$Body .= "First Name: ";
$Body .= $fname;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $lname;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
所以我可以打开模态,在填写详细信息后,我点击“提交”按钮。没有任何反应也可以将Google reCaptcha添加到此表单中,如果是,我应该将其添加到JS或PHP。如果我将它添加到PHP,我将不得不对JS文件进行任何更改。非常感谢您的回复。
答案 0 :(得分:0)
只需更改/替换上述代码的ajax部分即可:
$(function (){
$("#contact").on("submit", function(e){
e.preventDefault();
submitForm();
});
});
要:
$(function (){
$("#contact").submit(function(e){
submitForm();
e.preventDefault();
});
});