简体   繁体   English

提交后隐藏的jQuery联系人表单

[英]Jquery contact form hiding after submit

I have a contact form which is hidden when the page loads. 我有一个联系表单,该页面在加载时会隐藏。 The contact form can then be viewed by clicking the contact form button, causing it to slideUp and slideDown. 然后,可以通过单击“联系表单”按钮来查看该联系表单,以使其向上滑动和向下滑动。 The problem is that when the form is submitted the page refreshes and if there is an error message or a success message it is hidden because the page has reloaded, you have to click on the 'contact form' button to see it. 问题在于,提交表单后,页面会刷新,并且如果由于重新加载页面而隐藏了错误消息或成功消息,则该页面将被隐藏,您必须单击“联系表单”按钮才能看到它。 I'm not great with jquery or php. 我对jquery或php不太满意。 Any help would be much appreciated. 任何帮助将非常感激。 Once the form is submitted I need the messages to appear. 提交表单后,我需要显示消息。

the website is http://www.carlisleironing.co.uk/index.php 该网站是http://www.carlisleironing.co.uk/index.php

My jquery is 我的jQuery是

$(document).ready(function () {
    $("#contactLink").click(function () {
        if ($("#contactForm").is(":hidden")) {
            $("#contactForm").slideDown("slow");
        } else {
            $("#contactForm").slideUp("slow");
        }
    });
});

add the following (just before the last }); 添加以下内容(在最后一个});之前}); in your question): 在您的问题中):

if ($('#contactForm #error').size() > 0){
    $('#contactForm').slideUp('slow'); // or just .show();
}

Test if the #error element is present and, if so, show the form. 测试是否存在#error元素,如果存在,则显示表单。 I'm not sure what your success message looks like but a similar test can also be one for that. 我不确定您的成功消息是什么样子,但是类似的测试也可以用于此。

As for the other answers: yes, you can do an AJAX submit but chances are (and I'm assuming context here) that's out of the scope of this question. 至于其他答案:是的,您可以进行AJAX提交,但是机会(我在这里假设背景)超出了此问题的范围。 That would involve special request handling and additional validation librar(y/ies) added. 这将涉及特殊的请求处理和附加的验证库(y / ies)。

You can use ajax to submit the form and show the result without reloading the page: 您可以使用ajax提交表单并显示结果,而无需重新加载页面:

$('form').submit(function() {
    $.post($(this).attr('action'), $(this).serialize(), function(data) {
        console.log(data);
    });
    return false;
});

I am assuming that when someone submits the form, the php page generates the errors, but the user is not able to see them, because the form is hidden by default on a page load. 我假设当有人提交表单时,php页面会生成错误,但用户无法看到它们,因为表单默认是在页面加载时隐藏的。

You can name the submit button. 您可以命名“提交”按钮。 If the user clicks the submit button, the value of the button is send with the get or post request. 如果用户单击“提交”按钮,则该按钮的值将与get或post请求一起发送。 You can then change the behaviour of your php page to not have the contact form hidden if the form was submitted. 然后,您可以更改php页面的行为,以便在提交表单时不隐藏联系表单。

<input type="submit" name="theSubmitButton" value="Submit!" />

In php $_GET['theSubmitButton'] (or $_POST if you are using a post request) will be set if the user submitted the form, and it will not be set if that was not the case. 在php中,如果用户提交了表单,则会设置$_GET['theSubmitButton'] (如果使用的是发布请求,则为$ _POST);如果不是这种情况,则不会设置。 You can use isset( $_GET['theSubmitButton'] ) to test if the user did submit the page and alter the class of the contact form accordingly. 您可以使用isset( $_GET['theSubmitButton'] )测试用户是否提交了页面并相应地更改了联系表单的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM