jQuery - 提交时隐藏表单

发布于 2024-11-26 01:18:32 字数 2893 浏览 0 评论 0原文

我想在成功提交后隐藏我的表单:

这是测试空间的链接:http: //www.bgv.co.za/testspace/contact_3.php

它是 PHP jQuery mongrel 的组合。目前,我正在使用 jQuery 验证器,并且添加了自定义脚本来更改包装 div 的输入字段上的类 - 作为显示必填字段的一种方式。

这是我在 PHP

<?php

$subject = "Website Contact Form Enquiry";

中的内容//如果表单已提交 if(isset($_POST['提交'])) {

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = '[email protected]'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}

} ?>

这是脚本区域:

$(document).ready(function(){
$('#contactform').validate({
        showErrors: function(errorMap, errorList) {

            //restore the normal look
            $('#contactform div._required').removeClass('_required').addClass('xrequired');

            //stop if everything is ok
            if (errorList.length == 0) return;

            //Iterate over the errors
            for(var i = 0;i < errorList.length; i++)
                $(errorList[i].element).parent().removeClass('xrequired').addClass('_required');
        }

}); });

因此,当有人提交有效表单时,您会在“联系表单”下方看到一个新标题 - 来自此处:

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
    <h1 class="success_form">Thank You!</h1>
    <?php } ?>  

下面是输入表单的数据。

但我想隐藏下面的表单......请帮忙:)

这是我添加的新位,感谢 Flashbackzoo

        $("#content").empty();
    $("#content").append(
    "<p>If you want to be kept in the loop...</p>" +
    "<p>Or you can contact...</p>"
    );
    $('h1.success_').removeClass('success_').addClass('success_form');
    $('#contactform').hide();

},

I would like to hide my form on successful submission:

Here is a link to the testspace: http://www.bgv.co.za/testspace/contact_3.php

Its a combination PHP jQuery mongrel. At the moment I'm using jQuery validator and I have added custom script to change the class on the input fields wrapping div - as a way to show required fields.

Here is what I have in the PHP

<?php

$subject = "Website Contact Form Enquiry";

//If the form is submitted
if(isset($_POST['submit'])) {

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = '[email protected]'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}

}
?>

and here is the script area:

$(document).ready(function(){
$('#contactform').validate({
        showErrors: function(errorMap, errorList) {

            //restore the normal look
            $('#contactform div._required').removeClass('_required').addClass('xrequired');

            //stop if everything is ok
            if (errorList.length == 0) return;

            //Iterate over the errors
            for(var i = 0;i < errorList.length; i++)
                $(errorList[i].element).parent().removeClass('xrequired').addClass('_required');
        }

});
});

So at the moment when someone submits a valid form you see a new heading just under Contact form - its from here:

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
    <h1 class="success_form">Thank You!</h1>
    <?php } ?>  

and below it is the data entered into the form.

But its the form shoing below that I want to hide.... Please help :)

Here is the new bit I added thanks to Flashbackzoo

        $("#content").empty();
    $("#content").append(
    "<p>If you want to be kept in the loop...</p>" +
    "<p>Or you can contact...</p>"
    );
    $('h1.success_').removeClass('success_').addClass('success_form');
    $('#contactform').hide();

},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-12-03 01:18:32

添加提交处理程序...

$('#contactform').validate({
    showErrors: function(errorMap, errorList) {
        // your code...
    },
    submitHandler: function() {
        $('#contactform').hide();
    }
});

更新

响应您的评论布雷特。您可以将您的submitHandler更改为类似...

submitHandler: function() {
    $("#content").empty();
    $("#content").append(
        "<p>If you want to be kept in the loop...</p>" +
        "<p>Or you can contact...</p>"
    );
}

这将使用.empty()方法从#content中删除所有子元素。然后使用 .append() 方法将元素添加到#content。您可以将 #content 替换为您喜欢的任何选择器。

Add a submit handler...

$('#contactform').validate({
    showErrors: function(errorMap, errorList) {
        // your code...
    },
    submitHandler: function() {
        $('#contactform').hide();
    }
});

UPDATE

In response to your comment Brett. You could change your submitHandler to something like...

submitHandler: function() {
    $("#content").empty();
    $("#content").append(
        "<p>If you want to be kept in the loop...</p>" +
        "<p>Or you can contact...</p>"
    );
}

This will remove all child elements from #content using the .empty() method. Then add elements to #content using the .append() method. You can swap out #content for any selector you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文