php - 如何在提交到 3rd 方服务器之前保存数据?

标签 php javascript jquery ajax forms

我到处都在阅读大量问答,这些 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/

相关文章:

javascript - 使用 CSS 和 jQuery 的动态的、表格式的列表项行

jquery - 使用 JQuery 终端插件和 Django 与 jsonrpc 进行错误处理

php - 标记未显示在 map 上

javascript - 无法增加视频元素宽度

php - 根据来自不同表的 id 计算所有行

javascript - 如何修复 HTML5 canvas globalAlpha 问题?

javascript - 如何从javascript中的日期输入调用日期选择器?

jquery - "Tried to load angular more than once"警告 - 仅在初始页面加载时

php - 确定网页(或文本段落)的上下文/含义

php - PHP 中管道的非阻塞打开