联系人表格提交电子邮件不发送

时间:2020-09-30 20:25:54

标签: javascript php ajax

我使用的模板网站实际上已经完成,但是表单提交无效。 请问有人可以帮助我吗?

这是我的基本html:

<form action="#" class="form-contact" id="contactForm">
  <div class="row">
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_name" placeholder="* Nome" required="">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="email" class="form-control" id="p_email" placeholder="* Email" required="">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_phone" placeholder="* Telefone">
        <div class="help-block with-errors"></div>
      </div>
    </div>
    <div class="col-sm-6 col-md-6">
      <div class="form-group">
        <input type="text" class="form-control" id="p_subject" placeholder="* Assunto">
        <div class="help-block with-errors"></div>
      </div>
    </div>
  </div>
  <div class="form-group">
    <textarea id="p_message" class="form-control" rows="6" placeholder="* Mensagem"></textarea>
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <div id="success"></div>
    <button type="submit" class="btn btn-primary">ENVIAR</button>
  </div>
</form>

这是我的form-process.php:

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// SUBJECT
if (empty($_POST["subject"])) {
    $errorMSG .= "Subject is required ";
} else {
    $subject = $_POST["subject"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "myemail@outlook.com";
$Subject = $subject;

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$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;
    }
}

?>

这是我发送电子邮件的ajax / javascript:

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


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#p_name").val();
    var email = $("#p_email").val();
    var subject = $("#p_subject").val();
    var message = $("#p_message").val();

    $.ajax({
        type: "POST",
        url: "php/form-process.php",
        data: "name=" + name + "&email=" + email + "&subject=" + subject + "&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";
    }
    $("#success").removeClass().addClass(msgClasses).text(msg);
}

我一直无法发送表格,我总是收到错误消息“您是否正确填写了表格?”

0 个答案:

没有答案