我到处都在阅读大量问答,这些 stackoverflow 帖子似乎与我正在尝试做的事情最相关: 1) How we can save data on two servers using one sumit form? 2) How to call server side action method just before submitting form to 3rd party url?
基本上,我正在使用 Aweber 自动回复服务,但我遇到了一些技术问题,即使我在我的分析软件上看到人们正在用电子邮件填写表格并点击提交按钮。
因此,我希望在将表单数据提交给 Aweber 之前,我可以先在 TXT 文件中捕获服务器上的表单数据。
(根据我的研究,我需要 ajax、jquery 来实现这一点)
根据不同的帖子和教程,我想出了以下方法,但不幸的是仍然无法正常工作...
如果可能,请告诉我如何修复此代码。非常感谢!!!
使用 jquery:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//if submit button is clicked
$('#submit').submit(function () {
//Get the data from all the fields
var email = $('input[name=email]');
var custom_sub1 = $('input[name=custom sub1]');
var custom_sub2 = $('input[name=custom sub2]');
var custom_sub3 = $('input[name=custom sub3]');
//organize the data properly
var data = 'email=' + email.val() + '&custom_sub1=' + custom_sub1.val() + '&custom_sub2='
+ custom_sub2.val() + '&custom_sub3=' + custom_sub3.val();
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "http://mydomain.com/form_plus_email.php",
//method
type: 'POST',
//pass the data
data: data,
//Do not cache the page
cache: false,
success: function() {
}
});
//cancel the submit button default behaviours
return false;
});
});
</script>
</head>
以上,不知道是不是“return false”;导致问题
具有形式的主体:
<form method="post" action="http://www.aweber.com/scripts/addlead.pl">
<input type="text" name="email" value="Enter email" id="email">
<input value="xxxxxxxxxxxx" name="meta_web_form_id" type="hidden">
<input value="" name="meta_split_id" type="hidden">
<input value="xxxxxxxxxxxxx" name="listname" type="hidden">
<input value="http://domain.com/thankyoupage" name="redirect" type="hidden">
<input value="http://domain.com/thankyoupage" name="meta_redirect_onlist" type="hidden">
<input value="xxxxxxxxxxxxx" name="meta_adtracking" type="hidden">
<input value="1" name="meta_message" type="hidden">
<input value="email" name="meta_required" type="hidden">
<input value="1" name="meta_forward_vars" type="hidden">
<input value="" name="meta_tooltip" type="hidden">
<script type="text/javascript">
{
document.write('<input type="hidden" name="custom sub1" value="'+sub1+'">')
document.write('<input type="hidden" name="custom sub2" value="'+sub2+'">')
document.write('<input type="hidden" name="custom sub3" value="'+sub3+'">')
}
</script>
<input type="image" value="Submit Button" name="submit" src="image.png" id="submit" class="button1">
</form>
</body>
对于 Aweber,重要的是所有列出的隐藏和未隐藏的字段都将传递给 action="http://www.aweber.com/scripts/addlead.pl"。
但是,对于我自己来说,因为我只对 4 个字段感兴趣,所以我在 head 标记的 jquery 部分中指定了我需要的所有内容。
我不知道为什么代码不工作...所以,我如何确保在表单数据提交给第 3 方之前先使用 ajax 将其保存到我的服务器网址?现在……
<form method="post" action="http://www.aweber.com/scripts/addlead.pl">
正在正常执行和工作...但是据我所知,ajax 根本不保存数据
非常感谢!
最佳答案
首先更改您使用的提交按钮上的事件。所以不是
$('#submit').submit(function () {
使用
$('#submit').click(function () {
因为提交事件是针对表单的,而不是针对按钮的。更改后,如 Ramengo 所述,使用您的成功功能提交表单。
更好的方法是使用 Aweber API,它允许您自 2011 年 11 月以来将用户添加到帐户。
关于php - 如何在提交到 3rd 方服务器之前保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8627392/