简体   繁体   中英

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.

<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:

 $(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
$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. If I add it to PHP will I have to make any changes on JS file. Thank you so much for the response.

It's simple just change/replace your ajax part of above code :

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

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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