无法使用 Fancybox 获取要通过 AJAX 发布的表单数据

发布于 2025-01-01 06:59:03 字数 1157 浏览 4 评论 0原文

我使用 Fancybox 在单击链接时弹出联系表单。然后它将表单数据 POST 到 php 文件,发出一封电子邮件并返回一条成功消息。

提交表单后,页面重新加载,数据似乎无处可去。如果我在不使用 AJAX 的情况下提交表单,它工作正常,但随后会加载一个新页面。

形式:

<div style="display:none">
     <div id="questions">
       <form id="question-form" action="" method="POST">
         <p>Name</p> <input type="text" name="name">
         <p>Email</p> <input type="text" name="email">
         <p>Item</p> <input type="text" name="item">
         <p>Message</p><textarea name="message" rows="6" cols="25"</textarea>
         <br/>
         <input type="submit" value="Send">
      </form>
    </div>
</div>

脚本

$("#question-form").bind("submit", function() {

    $.fancybox.showActivity();

    $.ajax({
    type        : "POST",
    cache       : false,
    url         : "/includes/question-mailer.php",
    data        : $(this).serializeArray(),
    success     : function(data) {
                      $.fancybox(data);
                  }
});

return false;
});

我做错了什么?

I'm using Fancybox to have a contact form pop up when a link is clicked. Then it POSTs the form data to a php file, an email goes out and a success message comes back.

After I submit my form is that the page reloads and the data seems to go nowhere. If I submit the form without using AJAX it works fine but then loads a new page.

Form:

<div style="display:none">
     <div id="questions">
       <form id="question-form" action="" method="POST">
         <p>Name</p> <input type="text" name="name">
         <p>Email</p> <input type="text" name="email">
         <p>Item</p> <input type="text" name="item">
         <p>Message</p><textarea name="message" rows="6" cols="25"</textarea>
         <br/>
         <input type="submit" value="Send">
      </form>
    </div>
</div>

Script

$("#question-form").bind("submit", function() {

    $.fancybox.showActivity();

    $.ajax({
    type        : "POST",
    cache       : false,
    url         : "/includes/question-mailer.php",
    data        : $(this).serializeArray(),
    success     : function(data) {
                      $.fancybox(data);
                  }
});

return false;
});

What am I doing wrong?

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

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

发布评论

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

评论(1

︶ ̄淡然 2025-01-08 06:59:03

这是一个简单的错误,我将上面的脚本放在自己的

<script type="text/javascript">
$(document).ready(function(){
    $("a.lightbox").fancybox({
        'transitionIn'  :   'fade',
        'transitionOut' :   'fade',
        'speedIn'       :   600, 
        'speedOut'      :   200, 
        'overlayShow'   :   false
    });

    $("#question-form").bind("submit", function() {

        $.fancybox.showActivity();

        $.ajax({
            type        : "POST",
            cache       : false,
            url         : "/includes/question-mailer.php",
            data        : $(this).serializeArray(),
            success     :function(data){
                            $.fancybox(data);
                         }
        });

        return false;
    });

});     
</script>

It was a simple mistake, I was putting the script above in its own <script> tags instead of in the main fancybox attach script. My final code is:

<script type="text/javascript">
$(document).ready(function(){
    $("a.lightbox").fancybox({
        'transitionIn'  :   'fade',
        'transitionOut' :   'fade',
        'speedIn'       :   600, 
        'speedOut'      :   200, 
        'overlayShow'   :   false
    });

    $("#question-form").bind("submit", function() {

        $.fancybox.showActivity();

        $.ajax({
            type        : "POST",
            cache       : false,
            url         : "/includes/question-mailer.php",
            data        : $(this).serializeArray(),
            success     :function(data){
                            $.fancybox(data);
                         }
        });

        return false;
    });

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