【问题标题】:How to get around the FormSpree redirect?如何绕过 FormSpree 重定向?
【发布时间】:2015-11-29 15:48:49
【问题描述】:

我在 Github Pages Service 上获得了我的网站。我正在尝试实施 FormSpree 免费联系表格,但在您提交表格后,它会将您重定向到不同的网站。我想避免的事情。所以我在网上查了一下,当然其他人也想摆脱它(我在下图中省略了我的电子邮件)。

这是我上面得到的表格,但它实际上根本不起作用。不过,在我尝试摆弄它之前它就起作用了。

这是 FormSpree 中默认的表单外观:

<form action="//formspree.io/your@email.com"
      method="POST">
    <input type="text" name="name">
    <input type="email" name="_replyto">
    <input type="submit" value="Send">
</form>

这是我的版本(在我尝试绕过重定向之前它运行良好)

<div class=modal-body style="background-color: #454545">
    <p>Please use the form below to contact us regarding feedback or any questions you may have!
        We will never use the information given below to spam you and we will never pass on your
        information to a 3rd party.</p>
    <p>As we are using <a target="_blank" href="http://formspree.io">FormSpree</a> for this form
    please consult their privacy policy for any questions regarding this matter.</p>
    <form id="contactform" method="POST">
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">Name</span>
                <input name="form-name-input" type="text" name="name" class="form-control" required>
            </div>
            <div class="input-group">
                <span class="input-group-addon">Email</span>
                <input name="form-email-input" type="email" name="_replyto" class="form-control" required>
            </div>
            <div class="input-group">
                <span class="input-group-addon">Subject</span>
                <input name="form-subject-input" type="text" name="subject" class="form-control" required>
            </div>
            <div class="input-group">
                <span class="input-group-addon">Description</span>
                <textarea name="form-description-input" name="description" class="form-control" rows="4" cols="60" required></textarea>
            </div>
            <input type="text" name="_gotcha" style="display:none" />
            <input class="btn btn-success" data-dismiss="modal" type="submit" id="form-submit-btn" value="Submit">
            <input type="hidden" name="_next" value="http://www.dynamicrealities.net" onclick="FormSentConfirmation()"/>
        </div>
        <script>
            var contactform =  document.getElementById('contactform');
            contactform.setAttribute('action', '//formspree.io/' + 'dynamicrealities@gmail.com');
        </script>
    </form>
</div>
<div class="modal-footer" style="background-color: #333333">
    <button class="btn btn-danger btn-bg" data-dismiss="modal" type="button" id="form-dismiss-btn" onclick="">Close</button>
</div>

当我调用_next 时,我希望它执行以下警报:

function FormSentConfirmation() {
    alert('Thanks for the email, we\'ll be in touch promptly.');
}

当我按下提交按钮时,所发生的只是表单消失了,但我没有收到任何电子邮件。我可能只是做错了,因为我对 HTML/JavaScript 还是很陌生。

【问题讨论】:

  • 我不知道 FormSpree,但你应该能够检查页面是否被重定向,然后是 return false;,你应该很高兴。
  • 我已经添加了他们给你的标准表格作为例子。只要您将其放入该结构并在表单上调用action="//formspree.io/your@email.com" method="POST",FormSpree 就会完成剩下的工作。
  • 检查 www.enformed.io,就像 formspree 一样工作,但有一个开箱即用的重定向
  • @Ivangrx 不是它只适用于黄金和铂金计划......
  • 正如@eggrobot78 所说,它现在仅适用于付费计划。有关详细信息,请参阅stackoverflow.com/questions/40831948/…

标签: javascript html forms


【解决方案1】:

更新:该功能仅适用于付费计划。

在表单提交后使用表单内的这个隐藏输入字段进行重定向。您可以使用此代码块将用户重定向到您喜欢或您创建的页面。

<input type="hidden" name="_next" value="//site.io/thanks.html" />

value 替换为有效的感谢页面 URL。

点击此链接获取有关 how to use Formspreevideo demo 的教程

【讨论】:

  • 有没有办法在 Reactjs 中使用它?
  • 这个功能似乎只适用于特殊计划help.formspree.io/hc/en-us/articles/…
  • 我制作了一个不同的表单解决方案,称为 Web3Forms。它具有设计的重定向功能。它对开发人员来说更容易,并且有大量的免费层。
【解决方案2】:

在底部关注他们的 AJAX 示例:https://formspree.io/

$("#sendMessage").on("click", function() {
    $.ajax({
        url: "//formspree.io/dynamicrealities@gmail.com", 
        method: "POST",
        data: {message: "hello!"},
        dataType: "json"
    });
});

记住要包含 jQuery 以使其正常工作。删除:

var contactform =  document.getElementById('contactform');
contactform.setAttribute('action', '//formspree.io/' + 'dynamicrealities@gmail.com

并用我的代码替换它并更改选择器。或者使用$("form").on("submit"...

这是我的示例,它会向您发送一封邮件并提示用户提醒:http://jsfiddle.net/228d4snb/

在那之前做任何你想做的事return false;

【讨论】:

  • 哦,好吧。我完全错过了。我马上试试。谢谢!
  • @Vipar 在我们说话时尝试建立一个例子:P
  • 所以它可以工作,但是现在它只发送“你好!”到我的邮箱:P 我是否必须自己收集每个表单元素的值,然后将其放入数组中发送?我想这将是最好的解决方案。
  • @Vipar 是的,这将是最好的选择。我在我的中包含了一个$.serialize() 示例,它基本上将所有表单数据作为GET 发送,因此您可以看到消息中的内容。你应该能够把它放到一个更好的视野中!我很高兴能帮上忙!
  • @Vipar 您应该为您要查找的内容添加一些 ID。 $('#contactform').find('subject') 非常糟糕。您应该简单地添加 ID 并选择它们。
猜你喜欢
  • 2019-12-09
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 2013-08-22
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
相关资源
最近更新 更多