简体   繁体   English

使用一个提交按钮对表单提交执行两个操作

[英]Execute two actions on form submit with one submit button

I have an html form on my site, so that users can subscribe to my mailing list.我的站点上有一个 html 表单,这样用户就可以订阅我的邮件列表。 I'm using getResponse to build my list and send out automated emails.我正在使用 getResponse 来构建我的列表并发送自动电子邮件。 To do this, I put " https://app.getresponse.com/add_subscriber.html " into the action attribute of my form.为此,我将“ https://app.getresponse.com/add_subscriber.html ”放入表单的 action 属性中。 Here is a simplified version of my form:这是我的表格的简化版本:

<form action="https://app.getresponse.com/add_subscriber.html" method="post">
    <input type="text" name="name" id="name" class="required">
    <input type="email" name="email" id="email">
    <input type="submit" name="submitBtn" value=“Subscribe”>
</form> 

I also have a send.php file to insert user information into a MySQL database on my server.我还有一个 send.php 文件,用于将用户信息插入到我服务器上的 MySQL 数据库中。 But since I can only specify one file in the action attribute, I don't know have to execute both getResponse script and my own script when the user clicks on Subscribe.但是由于我只能在 action 属性中指定一个文件,所以当用户单击订阅时,我不知道必须同时执行 getResponse 脚本和我自己的脚本。

Basically I want to achieve the following behavior: When somebody clicks on "Subscribe", their information should get send to getResponse into my mailing list AND into the MySQL DB on my server.基本上我想实现以下行为:当有人点击“订阅”时,他们的信息应该发送到 getResponse 到我的邮件列表和我服务器上的 MySQL 数据库中。

I have spend hours looking online, I haven't found anything that works.我花了几个小时在网上看,我还没有找到任何有用的东西。 Thank you in advance.先感谢您。

Give your form an id say myForm给你的表格一个 id 说myForm

<form action="https://app.getresponse.com/add_subscriber.html" method="post" id="myForm">

Then intercept the form's submit event and prevent the submission to app.getresponse.com, instead call ajax to your send.php passing the form data and once send.php returns submit the form to app.getresponse.com然后拦截表单的提交事件并阻止提交到 app.getresponse.com,而是调用 ajax 到你的send.php传递表单数据,一旦send.php返回提交表单到 app.getresponse.com

<script>
$(document).ready(function(){ 

    $("#myForm").submit(function(e){
      e.preventDefault();
      e.stopPropagation();
      //don't submit now

      //do ajax first
      $.ajax({url: '/send.php', method: $("#myForm").attr('method'), data: $("#myForm").serialize() , success: function(){
        $("#myForm").submit(); //now submit to app.getresponse.com
      }});
    });

});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM