简体   繁体   English

在我使用Ajax和PHP构建的表单中,单击“提交”按钮后没有任何反应

[英]Nothing happens after I click on submit button, in my form built in Ajax and PHP

I am not sure what am I doing wrong, I have contact us form which sends an email. 我不确定自己在做什么错,我已经与我们联系并发送了电子邮件。 But nothing happens after I click on Submit. 但是单击“提交”后没有任何反应。 I am using Materialize CSS for UI. 我正在使用Materialize 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>

I am also using Ajax which is as follows: 我也在使用如下的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);
} 

And finally PHP code 最后是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;
     }
 }
?>

So I am able to open the Modal and after filling the details I click on Submit button. 因此,我可以打开模式,并在填写完详细信息后单击“提交”按钮。 Nothing happens also can I add Google reCaptcha to this form if yes should I add it to JS or PHP. 如果可以,我也无法将Google reCaptcha添加到此表单,如果可以,我可以将其添加到JS或PHP。 If I add it to PHP will I have to make any changes on JS file. 如果将其添加到PHP,则必须对JS文件进行任何更改。 Thank you so much for the response. 非常感谢您的回复。

It's simple just change/replace your ajax part of above code : 很简单,只需更改/替换上面代码的ajax部分:

$(function (){
    $("#contact").on("submit", function(e){
        e.preventDefault();
        submitForm();
    });
});

To: 至:

$(function (){
    $("#contact").submit(function(e){
        submitForm();
        e.preventDefault();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM