与服务器端PHP处理程序的脱机联系表单未显示成功或错误消息

时间:2018-04-23 11:18:08

标签: php jquery ajax forms

我有一个联系表单,在服务器上运行时效果很好。但是我试图只保留服务器上的handler.php和其他文件离线。到目前为止,它一直在发送电子邮件。唯一的问题是,在电子邮件成功发送后,它没有显示成功消息,这在所有文件都在线时都不是问题。

所以我正在尝试使用其中的表单制作一个html文件,我可以让人们在浏览器中本地打开它。然后,在他们点击提交后,数据将被发送到在线服务器上的php站点。

我应该更新什么才能从电子邮件流程中获取成功或错误消息?提前感谢您的建议。

form-scripts.js

$("#contactForm").validator().on("submit", function(event) {
    if (event.isDefaultPrevented()) {
        formError();
        submitMSG(false, "Did you fill in the form properly?");
    } else {
        event.preventDefault();
        submitForm();
    }
});


function submitForm() {
    var name = $("#name").val();
    var email = $("#email").val();
    var message = $("#message").val();

    $.ajax({
        type: "POST",
        url: "http://example.com/form-process.php",
        data: "name=" + name + "&email=" + email + "&message=" + message,
        success: function(text) {
            if (text == "success") {
                formSuccess();
            } else {
                formError();
                submitMSG(false, text);
            }
        }
    });
}

function formSuccess() {
    $("#contactForm")[0].reset();
    submitMSG(true, "Message Submitted!")
}

function formError() {
    $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
        $(this).removeClass();
    });
}

function submitMSG(valid, msg) {
    if (valid) {
        var msgClasses = "h3 text-center tada animated text-success";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}

这是服务器上的form-process.php

<?php
$errorMSG = "";

$name    = $_POST["name"];
$email   = $_POST["email"];
$message = $_POST["message"];

$EmailTo = "example@gmail.com";
$Subject = "New Message Received";

$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

$success = mail($EmailTo, $Subject, $Body, "From:" . $email);

// redirect to success page
if ($success && $errorMSG == "") {
    echo "success";
} else {
    if ($errorMSG == "") {
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

以下是表格:

<form role="form" id="contactForm" data-toggle="validator" class="shake">
   <div class="row">
      <div class="form-group col-sm-6">
         <label for="name" class="h4">Name</label>
         <input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
         <div class="help-block with-errors"></div>
      </div>
      <div class="form-group col-sm-6">
         <label for="email" class="h4">Email</label>
         <input type="email" class="form-control" id="email" placeholder="Enter email" required>
         <div class="help-block with-errors"></div>
      </div>
   </div>
   <div class="form-group">
      <label for="message" class="h4 ">Message</label>
      <textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
      <div class="help-block with-errors"></div>
   </div>
   <button type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
   <div id="msgSubmit" class="h3 text-center hidden"></div>
   <div class="clearfix"></div>
</form>

0 个答案:

没有答案