表单成功和表单验证消息的函数不同

Different function for form success and for the form validation messages

本文关键字:表单 函数 消息 成功 验证      更新时间:2023-09-26

我刚刚得到了一些函数和回调的帮助,以便在我的表单上获得这个动画,一旦提交:

            $("#message").show().delay(5000).fadeOut('fast', function(){
            $("#slide_panel").slideToggle("slow");
});

虽然,我现在的问题是,如果有人不得不提交表单而没有输入正确的详细信息,错误消息也会弹出(弹出在相同的div "message"作为感谢消息),延迟5秒,然后关闭表单。

当然,我不希望它关闭表单,而是显示错误消息5秒,然后淡出错误消息。

还有什么需要补充的吗:

function(data){
            document.getElementById('message').innerHTML = data;
            $('#message').slideDown('slow');
            $('#contactform img.loader').fadeOut('fast',function()
{$(this).remove()});
            $('#submit').removeAttr('disabled');
            if(data.match('success') != null);
            $('#name').val( "" );
            $('#email').val( "" );
            $('#phone').val( "" );
            $('#dayin').val( "" );
            $('#dayout').val( "" );
            $('#comments').val( "" );
            $('#verify').val( "" );
            $("#message").show().delay(5000).fadeOut('fast', 
function(){
            $("#slide_panel").slideToggle("slow");
});
        }
    );
    });
    return false;
});
});

我假设我需要做一些类似于下面代码的事情:

if(data.match('success') != null);

在我的contact.php表单....我有这个:

if (isset($_POST['verify'])) :
    $posted_verify   = $_POST['verify'];
    $posted_verify   = md5($posted_verify);
else :
    $posted_verify = '';
endif;
// Important Variables
$session_verify = $_SESSION['verify'];
if (empty($session_verify)) $session_verify = $_COOKIE['verify'];
$error = '';
    if(trim($name) == '') {
        $error .= '<li>Your name is required.</li>';
    }
    if(trim($email) == '') {
        $error .= '<li>Your e-mail address is required.</li>';
    } elseif(!isEmail($email)) {
        $error .= '<li>You have entered an invalid e-mail address.</li>';
    }
    if(trim($phone) == '') {
        $error .= '<li>Your phone number is required.</li>';
    } elseif(!is_numeric($phone)) {
        $error .= '<li>Your phone number can only contain digits.</li>';
    }
    if(trim($comments) == '') {
        $error .= '<li>You must enter a message to send.</li>';
    }
    if($session_verify != $posted_verify) {
        $error .= '<li>The verification code you entered is incorrect. 
</li>';
    }
    if($error != '') {
        echo '<div class="error_message">Attention! Please correct the  
errors below and try again.';
        echo '<ul class="error_messages">' . $error . '</ul>';
        echo '</div>';
    } else {
    if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

我还需要做什么吗?或者我只需要编辑javascript文件?

如果你使用JSON调用后面代码中的某个函数,你将能够返回一个状态属性。

我在我当前的项目中也使用了它,下面是我如何使用它的一个例子:

var link = '/brainbattleJSON/MarkAssignmentAsInProgress';
                $(this).hide();
                $.getJSON(link, { questionId: qId }, function (json) {
                   if(json.status == "ok"){
                   //do this
                   }else{
                   //do this
                   }
                });

背后的代码:

// your function with validation
// if the form is valid make status "ok" otherwise put anything else
Return Json(New With {.status = "ok"});

我希望这能对你有所帮助:)

编辑:

您需要将var link的值更改为检查表单的函数路径。那么你现在在哪里说如果你错了!= "你将返回json。在这里,您将输入:

return json(new with{.status = 'error', .errors = 'yourErrors'})

所以对于错误,发送一个数组可能是有用的,以防你在表单上得到超过1个错误。所有的消息将不再显示在php与echo,但你将不得不把错误在那里与javascript。

我有一个上传的注册和登录页面(zip文件)在以下链接:http://www.4shared.com/folder/uanQHCAg/_online.html

它使用相同的div来显示成功和错误消息。您可以尝试实现您正在寻找的,添加淡出效果。