2015-02-24 732 views
0

我的网站很简单,它有文字说“点击这里”,点击 将在屏幕中心打开一个模式类型的联系表单。如何在成功提交后关闭弹出窗口

当前表单提交时,它延迟1秒,然后调用submit.php 将数据发送到我的电子邮件。

而不是重定向到submit.php,我想保持在同一页面上,并简单地关闭弹出式联系表格 。

因此,它应该是这样的:

点击“点击这里”>详细填写弹出形式>点击提交>表单提交后1秒钟,然后完全关闭。(无需重新直接)

您可以查看我的演示站点here.

下面是HTML表单:

<form method="post" action="submit.php" id="contactform" class="signin"> 
<h1>On submit, this window should close</h1> 

     <input name="name" id="name" type="text" class="feedback-input" placeholder="Name" /> 
     <input name="email" id="email" type="text" class="feedback-input" placeholder="Email" required pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)" /> 
     <div class="antispam"> 
     <br /><input name="url" type="hidden" /></div> 
     <textarea name="message" id="message" class="feedback-input" placeholder="Write away!" required></textarea> 

<button id="flybutton"> 
      <p>Submit here</p> 
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"> 
       <path id="paper-plane-icon" d="M462,54.955L355.371,437.187l-135.92-128.842L353.388,167l-179.53,124.074L50,260.973L462,54.955z 
M202.992,332.528v124.517l58.738-67.927L202.992,332.528z"></path> 
      </svg> 

     </button> 
</form> 

你可以看到表单ID是“联系形式”和按钮ID为“flybutton”

以下脚本目前使用此数据,延迟1秒,提交表单,然后将 重定向到submit.php。

相反,我需要它延迟1秒,提交表单,然后关闭弹出窗口(和黑色背景)。

目前我有一个脚本,将通过Escape键关闭窗体,所以也许这可能以某种方式实现? 我觉得我的脚本需要以某种方式组合。

这里是我的脚本有:

1秒的延迟+提交表单

<script> 
     var $btn = $('#flybutton'); 
     $("#contactform").validate({ 
      submitHandler: function (form) { 
       fly(form); 
      } 
     }); 

     $btn.on('fliyingEnd', function (e, form) { 
      form.submit(); 
     }) 

     function fly(form){ 

      $btn.toggleClass('clicked'); 
      $btn.find('p').text(function(i, text) { 
       return text === "Fire!" ? "Fire!" : "Fire!"; 
      }); 

      setTimeout(function() { 
       $btn.trigger('fliyingEnd', [form]); 
      }, 1000); 

     } 
    </script> 

关闭格式框(#登录框)和黑色背景(#mask)通过ESC键:

<script type="text/javascript"> 
$(document).keyup(function(e) { 
    if (e.keyCode == 27) { 
     $("#mask").fadeOut(300); 
    } 
    if (e.keyCode == 27) { 
     $("#login-box").fadeOut(300); 
    } 
}); 
</script> 

其他用户帮助这个脚本都Ø我不知道它的目的:

<script type="text/javascript"> 
$(document).ready(function() { 
$('a.login-window').click(function() { 

      //Getting the variable's value from a link 
    var loginBox = $(this).attr('href'); 

    //Fade in the Popup 
    $(loginBox).fadeIn(300); 

    //Set the center alignment padding + border see css style 
    var popMargTop = ($(loginBox).height() + 24)/2; 
    var popMargLeft = ($(loginBox).width() + 24)/2; 

    $(loginBox).css({ 
     'margin-top' : -popMargTop, 
     'margin-left' : -popMargLeft 
    }); 

    // Add the mask to body 
    $('body').append('<div id="mask"></div>'); 
    $('#mask').fadeIn(300); 

    return false; 
}); 
// When clicking on the button close or the mask layer the popup closed 
    $('a.closest, #mask').bind('click', function() { 
     $('#mask , .login-popup').fadeOut(300 , function() { 
     $('#mask').remove(); 
    }); 
    return false; 
    }); 
}); 
</script> 

能否请你告诉我,为了得到我所需要的功能要编辑的代码?

我无法得到的jsfiddle工作,但here's my demo site again

UPDATE: 我已经纳入AJAX,但我有一个问题。具体来说,表单提交但它仍然重定向。

包括在此我下​​面表格

<div id="result"></div> 

,这里是脚本..如何让我没有重新直接?

<script> 
$(document).ready(function() { 

    /* Attach a submit handler to the form */ 
$("#contactform").submit(function(event) { 

    /* Stop form from submitting normally */ 
    event.preventDefault(); 

    /* Clear result div*/ 
    $("#result").html(''); 

    /* Get some values from elements on the page: */ 
    var values = $(this).serialize(); 

    /* Send the data using post and put the results in a div */ 
    $.ajax({ 
     url: "submit.php", 
     type: "post", 
     data: values, 
     success: function(){ 
      alert("success"); 
      $("#result").html(''); 
     }, 
     error:function(){ 
      alert("failure"); 
      $("#result").html('An error occurred'); 
     } 
    }); 
}); 
}); 
</script> 

非常感谢!

+1

为了不“刷新”的页面,你需要使用AJAX调用 - 这将使一个请求,并没有从当前页面导航离去返回响应。你可以在这里找到更多:http://api.jquery.com/jquery.ajax/。标准表单POST(您目前正在执行的操作)总是需要重新加载页面 - 您总是可以重新加载相同的页面。 – Carl 2015-02-24 20:45:51

+0

您可以提供解决方案吗?现在任何事情都会有帮助。与此同时,我会研究AJAX。谢谢 – Mathomatic 2015-02-24 21:24:56

+0

没关系,我在这里找到了一个坚实的链接,应该有所帮助http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php/14217926#14217926 – Mathomatic 2015-02-24 21:34:30

回答

0

这就是现在被称为码1秒后用户点击提交:

$btn.on('fliyingEnd', function (e, form) { 
     form.submit(); 
    }) 

希望表单提交后摆脱屏幕变暗(面具)和登录弹出框,所以索性在两段代码添加到淡出这些元素出来:

$btn.on('fliyingEnd', function (e, form) { 
    form.submit(); 
    $('#mask').fadeOut(300); 
    $("#login-box").fadeOut(300); 
}) 
+0

谢谢,这是成功地删除弹出和黑色背景,但表单仍然提交到submit.php。我不需要重定向 – Mathomatic 2015-02-24 21:04:40

0

一个简单的事情是:

$(document).ready(function(){ 

$('#a.login-window').dialog({ 
    modal: true, 
    autoOpen: false, 
    buttons: { 
     Ok: function() { 
      $(this).dialog("close"); 
     } 
    } 
}); 
+0

我在其他脚本之外的脚本标记中添加了此代码,并且似乎有*无*更改。该表单仍然重定向到submit.php。我不认为我的弹出窗口是一个实际的模式,但我可能是错的。也许这就是为什么代码无法工作?我需要将表单数据提交到submit.php,但保留在原始页面上,同时关闭表单+黑色背景(#mask) – Mathomatic 2015-02-24 21:07:07

-1

我想你的演示链接,并在submit.php你存储数据(我猜测),并显示味精

Thank you for contacting us. 

We will get back to you within 24 hours. 


Go Back 

而是这条消息重定向页面的使用PHP函数demo4.html 。 header('Location: '.demo4.html);

如果你上传submit.php我会给出更多的细节。

+0

因此,您要说我应该重定向到submit.php,然后将BACK重定向到demo4.html?我正在寻找* no *重定向,而不是2!我需要将表单数据提交到submit.php,但保留在ORIGINAL页面上,同时关闭表单+黑色背景(#mask)。 – Mathomatic 2015-02-24 21:09:10

0

所以,如果我找到了你的权利,你想发送数据到你的服务器,而不必重定向文件。你应该看看的JavaScript XMLHttpRequest

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

所以,我会做这样的事情:

function submit(){ 
    var contactform=$("#contactform"); 
    var formdata=new FormData(contactform); //create a js FormData object 

https://developer.mozilla.org/en-US/docs/Web/API/FormData

var xhr=new XMLHttpRequest(); 
    xhr.onreadystatechange=function(){ 
    // do something here (this is an event handler that is invoked a few times during uploading/downloading process) 
    } 
    xhr.open("POST","submit.php"); 
    xhr.send(formdata); 
} 

那么你可以嵌入此像@Zach Sandlers回答

$btn.on('fliyingEnd', function (e, form) { 
    submit(); // here you send the data to the server 
    $('#mask').fadeOut(300); 
    $("#login-box").fadeOut(300); 
}) 

警告:braincompiled代码!可能包含语法错误!

希望帮助

+0

我不确定我们是否了解对方。当用户提交表单时,我希望表单数据被发送到submit.php但不重定向。它应该保持在同一个原始页面上,同时还要关闭表单(#登录框)和黑色区域(#mask)。我希望收到带有表单数据的电子邮件,同时也不会将用户从主页面(demo4.html) – Mathomatic 2015-02-24 21:13:48

+0

重定向到XML XMLHttpRequest支持几种方法,如GET和POST。因此您使用POST方法将表单内的数据发送到您的php脚本。这不会触发任何重定向。很抱歉,我无法帮助您使用#登录框和#mask,因为我对CSS没有经验 – mld 2015-02-24 21:20:30