【问题标题】:Contact form take you to new page联系表格带您进入新页面
【发布时间】:2015-11-05 16:09:03
【问题描述】:

我最近转换为 wordpress 网站的网站上有一个 ajax 联系表。联系表格工作正常,直到我将其转换为 wp 网站。现在它只需要输入您输入的信息并使用它创建一个新页面。我知道它发生了什么,但不知道从哪里开始。

You can check the page out here

有遇到类似情况的经验和/或任何解决方案吗?

【问题讨论】:

  • 在您的联系页面中head 之外有代码。此外,联系表单 javascript 不会阻止默认操作的发生。
  • 我需要添加什么来解决这个问题?

标签: php jquery ajax wordpress


【解决方案1】:

我不确定在 Wordpress 或 jQuery 中是如何完成的,但我认为您的 javascript 函数需要稍作更改,并且理想情况下代码需要在 <head></head> 部分中。

jQuery(document).ready(function($) {

    $("#ajax-contact-form").submit( function( event ) {
        /* Prevent the form actually submitting in standard manner */
        event.preventDefault();

        var str = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "/includes/contact-process.php",
            data: str,
            success: function(msg) {
                // Message Sent? Show the 'Thank You' message and hide the form
                if(msg == 'OK') {
                    result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
                    $("#fields").hide();
                } else {
                    result = msg;
                }
                $('#note').html(result);
            }
        });
        return false;
    });
});

而且,您需要在 ajax 函数中使用的路径也需要更改 - 已在上面进行了编辑。 (我刚才尝试时得到了 404)

【讨论】:

  • 与 Rasclatt 的解决方案相同。新页面加载问题已解决,但联系表单现在没有任何作用。
  • 您是否在实时站点上进行了更改以便我们查看?我可以看到函数已经移动,但你没有像我提到的那样编辑路径......它仍然返回 404......它应该是 /includes/contact-process.php
  • 好的,它仍然找不到脚本 - 我尝试了一些变体但找不到它
  • 我会支持你,因为你是对的,文件不在 ajax 的位置。
  • 我修好了。我只需要引用回 wordpress 文件夹而不是主题文件夹。非常感谢您的帮助!
【解决方案2】:

如果您查看控制台(查看页面上的源代码),您的 jQuery 表单 ajax 将出现在页面顶部,因此 jQuery 库正在加载之后 你已经声明了函数。应该在 &lt;head&gt; 结束之前和 jQuery 库被加载之前登陆:

....etc...
<script type="text/javascript" src="http://www.spincycleprodcution.com/wp-content/themes/spincycle/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">                                 
// we will add our javascript code here
jQuery(document).ready(function($) {

    $("#ajax-contact-form").submit(function() {
        var str = $(this).serialize();

        $.ajax({
            type: "POST",
            url: "includes/contact-process.php",
            data: str,
            success: function(msg) {
                // Message Sent? Show the 'Thank You' message and hide the form
                if(msg == 'OK') {
                    result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
                    $("#fields").hide();
                } else {
                    result = msg;
                }
                $('#note').html(result);
            }
        });
        return false;
    });
});
</script>
</head>

您当前的控制台错误是:

ReferenceError:找不到变量: jQuery(匿名函数)

因为您在加载 jQuery 库之前加载了函数,所以它无法找到 jQuery。您甚至可以在页脚中加载您的 ajax....或任何真正落在 jQuery 库之后的地方。

AJAX 工作后,您需要确保 ajax 可以找到引用页面:

contact-process.php

或者您收到此错误:

[错误] 加载资源失败:服务器响应状态为 404(未找到)(contact-process.php,第 0 行)

【讨论】:

  • 这会停止新页面加载问题,但联系表单现在没有任何作用。
  • 那是因为找不到你的includes/contact-process.php页面
  • 错误:Failed to load resource: the server responded with a status of 404 (Not Found) (contact-process.php, line 0)
  • 它被加载到它应该在的服务器上。
  • 您的 ajax 没有在您告诉它找到它的地方找到它。这就是为什么它现在不起作用。我复制了单击提交时收到的确切错误。
猜你喜欢
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 2018-06-30
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多