简体   繁体   English

提交后隐藏表单

[英]Hide Form After Submit

I am attempting to hide my form after my submit button has been pressed using Jquery. 在使用Jquery按下提交按钮后,我试图隐藏我的表单。

So far I have imported the Jquery library. 到目前为止,我已经导入了Jquery库。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>  

Trying to attempt to hide the form using the class "form-fields." 尝试使用“表单字段”类来隐藏表单。 This class holds the whole form. 这个类包含整个表单。

Trying to hide it like this: 试图像这样隐藏它:

 <?php if (isset($_POST['process']) && ($_POST['process'] == 1)): ?>
<script type="text/Javascript">
    $('#form-fields').hide(); 
</script>

This doesn't seem to be working. 这似乎不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

You need to hide the form using the submit event handler and need to remove the PHP condition <?php if (isset($_POST['process']) && ($_POST['process'] == 1)): ?> since it runs in the server side 您需要使用提交事件处理程序隐藏表单,并且需要删除PHP条件<?php if (isset($_POST['process']) && ($_POST['process'] == 1)): ?>因为它在服务器端运行

What happens below is, we register an event handler which will get called when the form is submitted, and inside that the form is hidden 下面发生的是,我们注册一个事件处理程序,它将在提交表单时调用,并在表单内部隐藏

<script type="text/Javascript">
    $('#form-fields').submit(function(){
        $(this).hide(); 
    })
</script>

If the form sends users to the same page after clicking submit, it's probably easiest to do the hiding with php. 如果表单在单击提交后将用户发送到同一页面 ,则最简单的方法是使用php进行隐藏。 Add this somewhere at the top of your file: 将其添加到文件顶部的某处:

<?php $submitPressed = isset($_POST['process'] && $_POST['process'] == 1; ?>

You can then wrap anything you want to hide in the following tags: 然后,您可以在以下标记中包含要隐藏的内容:

<?php if (!$submitPressed): ?>
<!-- form goes here -->
<?php endif; ?>

Note, this will only hide the form once the server has been notified; 请注意,这只会在服务器通知后隐藏表单; there might be a tiny delay after pressing submit. 按提交后可能会有一点延迟。

Otherwise, you'll want to use some jQuery bound to the submit event. 否则,您将要使用绑定到submit事件的一些jQuery。

$('form-fields').submit(function(){
    var $this = $(this); // so we can use in ajax callback
    $.ajax({
        url: '/',
        data: $(this).serialize(),
        success: function(data){
            if(data == true){
               $this.hide(); //hide form if we got a true to return
            }
        }
    });
    return false; //stop default form submit
});

Same page... 同页......

<?php 
if(isset($_POST['process']) && ($_POST['process'] == 1)):
    // do whatever processing
    return true; //at the end so we can compare in ajax
endif;     
?>
You can add the css property of that element to hidden on click..


$(function(){
 $('form-fields').submit(function(){
     $(this).css("visibility", hidden);
  });
});

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

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