邮件表单验证无效

时间:2018-03-09 01:41:19

标签: javascript php forms

我有这个联系表单正确发送邮件,但它没有正确地验证服务器端验证。当我提交一个空表单(或者已完成某些字段)时,它会显示正确的字段错误消息,但无论如何都会发送表单。

任何帮助都将不胜感激。

表格:

<form action="php/form-process.php" role="form" id="contactForm">

  <input type="text" id="name" name="name" class="form-control input-lg" placeholder="Nombre y Apellido *" data-error="Por favor, ingrese su nombre">
  <input type="text" id="phone" name="phone" class="form-control input-lg" placeholder="Teléfono *" data-error="Por favor, ingrese el teléfono">
  <input type="email" name="email" class="email form-control input-lg" id="email" placeholder="Email *" data-error="Por favor, ingrese su email">
  <input type="text" id="date" name="date" class="datepicker form-control input-lg" placeholder="Fecha del evento *" data-error="Por favor, ingrese una fecha">
  <textarea id="message" name="message" rows="7" placeholder="Mensaje *" class="form-control input-lg" data-error="Déjenos un mensaje"></textarea>
  <button type="submit" id="submit" class="faa-float animated-hover btn btn-lg btn-default btn-form"></i> ENVIAR</button>

  <div id="msgSubmit" class="h3 text-center hidden"></div> 
  <div class="clearfix"></div>   

</form>     

PHP:

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Nombre y Apellido requeridos | ";
} else {
    $name = $_POST["name"];
}

// PHONE
if (empty($_POST["phone"])) {
    $errorMSG .= "Teléfono requerido | ";
} else {
    $phone = $_POST["phone"];
}

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

// DATE
if (empty($_POST["date"])) {
    $errorMSG .= "Fecha requerido | ";
} else {
    $date = $_POST["date"];
}


// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Mensaje requerido";
} else {
    $message = $_POST["message"];
}


$EmailTo = "mail@domain.com";
$Subject = "Example subject";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Telefono: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Fecha: ";
$Body .= $date;
$Body .= "\n";
$Body .= "Mensaje: ";
$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;
    }
}


?>

和Javascript代码

$("#contactForm").validator().on("submit", function (event) {
    if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Verifique el formulario");
    } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
    }
});


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#name").val();
    var phone = $("#phone").val();
    var email = $("#email").val();
    var date = $("#date").val();
    var message = $("#message").val();


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

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

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 text-success";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}

0 个答案:

没有答案
相关问题