如何使用 jquery(ajaxForm 插件)将表单提交到外部站点,但不重新加载到其他页面?
我正在尝试创建一个表单,将客户的姓名和电子邮件地址发送到 icontact(电子邮件列表应用程序)。
icontact 的所有值都是正确的,如果我像往常一样提交表单,icontact 会回复说它添加了电子邮件地址。 成功消息将从 icontact 网站加载到新页面中。
我想要完成的是 ajax 将发送表单并在当前页面中返回成功消息,而不会将客户发送到新页面。
I'm trying to create a form that sends the customer's name and email address to icontact (an email list application).
All the values are correct for icontact and if I submit the form as usual, icontact responds that it added the email address. The success message loads in a new page from icontact's website.
What I'd like to accomplish is that ajax would send the form and returns a success message within the current page, without sending the customer to a new page.
Here is the test page: http://www.skhot.com/test/js-test/forms/index.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于那些无法通过 ajax 提交 icontact 表单的人,请尝试以下操作:
将 xxx 替换为所需参数:)
For those who are having trouble submitting the icontact form via ajax try this:
replace xxx with the required parameter :)
就您而言,我认为:
preventDefault 将阻止提交按钮的正常行为,即遵循表单的操作 URL。
如果这不起作用,您可以尝试在 success 函数中返回 false:
编辑:您可以尝试的另一件事是在 onsubmit 事件中附加“return false”:
In your case, I think:
preventDefault will prevent the submit button's normal behaviour, which is to follow the form's action URL.
If that doesn't work, you can try returning false in the success function:
EDIT: Another thing you can try is attaching 'return false' the onsubmit event:
感谢他的遮阳篷,不过做了一些更改,这就是我所拥有的
Thanks to for his awnser, made a few changes though, here is what I have
一种选择是放弃具有提交按钮的表单,而是使用按钮并具有 JavaScript onClick 处理程序。 在 onClick 处理程序中,您应该使用 AJAX 在 POST 请求中发送表单中的数据,然后捕获响应。 当您收到响应时,使用 jQuery 修改您想要修改的任何 div 表单,或者执行重定向,或者其他任何操作。
我认为真正的问题是,默认情况下表单提交总是在表单标记的 action 属性中加载新页面,因此您不能依赖 HTML 表单提交方法。
可能还有其他更有效的方法(特别是使用 jQuery,我对此不太了解),但我提出的算法应该可行。
One option will be to get rid of the form having a submit button, instead using a button and having a JavaScript onClick handler. In the onClick handler, you should use AJAX to send the data in the form in a POST request, and then capture the response. When you get the response, use jQuery to modify whatever div form you want to modify, or do a redirect, or whatever.
I think the real issue is, form submission by default just always loads that new page in the action attribute of the form tag, so you can't rely on an HTML form submission method.
There might be other, more efficient ways (especially using jQuery, which I don't know all that well), but the algorithm I've proposed should work.
请参阅 jQuery Ajax。
;
See jQuery Ajax.
;
Ajax 可以提交表单,但显示成功消息很棘手。
请参阅此 JQuery AJAX post 方法,
将类型参数设置为“html”。
您指定一个回调函数,该函数可以解析并显示来自 iContact 服务器的响应。
Ajax can submit the form but showing up a success message is tricky.
See this JQuery AJAX post method
set the type parameter as 'html'.
You specify a callback function that can parse and display the response from the iContact server.
我把 icontact 排除在外,一切都按计划进行。 所以问题不在于代码,而在于 icontact 如何处理他们的 php.ini。 我必须与他们联系以获得真正的解决方案。
感谢大家的帮助和提供建议! 如果没有你,我的故障排除就不可能成功。
I took icontact out of the equation and the everything worked as planed. So the problem is NOT the code but how icontact handles their php. I'll have to get in touch with them for a real solution.
Thanks everyone for helping and providing advice! My troubleshooting wouldn't have worked with out you.