简体   繁体   English

提交表单后保持同一页面,然后再次处理表单

[英]Staying on same page after the form is submitted and then process the form again

I'm integrating Marketo (3rd party marketing software) with one of our tools on our website. 我正在将Marketo(第三方营销软件)与我们网站上的一个工具集成在一起。

There is a form that calls an action " http://info.a10networks.com/index.php/leadCapture/save " after it is submitted and the form data is saved into Marketo: 提交后,有一个表单可以调用“ http://info.a10networks.com/index.php/leadCapture/save操作 ,并将表单数据保存到Marketo中:

<form
    class="lpeRegForm formNotEmpty"
    method="post"
    enctype="application/x-www-form-urlencoded"
    action="http://info.a10networks.com/index.php/leadCapture/save"
    id="mktForm_1225"
    name="mktForm_1225">

I want to use the same form data to store it in local database(MySQL) too. 我想使用相同的表单数据将其存储在本地数据库(MySQL)中。 Ideally, I'd like to load the same page after the form data is sent and also store this form data locally. 理想情况下,我希望在发送表单数据后加载相同的页面,并在本地存储此表单数据。

Is there a way to perform the following actions: 有没有办法执行以下操作:

  1. the form action is called and the data is sent to an external database 调用表单操作,并将数据发送到外部数据库
  2. load back the same page and store this form data locally into the database (be able to use $_POST ) 加载同一页面并将此表单数据本地存储到数据库中(能够使用$_POST

I'm using PHP and plain javascript for this integration. 我正在使用PHP和普通的javascript进行这种集成。 Please advise. 请指教。

You can do this using an ajax call to your own scripts, then submitting the form to marketo. 您可以使用ajax调用自己的脚本,然后将表单提交给marketo。

Essentially if you want to capture the data before its sent off to a remote server for processing you'll capture the data first then allow the form to submit and do its intended processing afterwards. 基本上,如果您希望在将数据发送到远程服务器进行处理之前捕获数据,则首先捕获数据,然后允许表单提交并在之后执行其预期的处理。

Here we capture the click of the form, then make sure to disable the button by adding a class to it. 在这里我们捕获表单的单击,然后确保通过向其添加类来禁用该按钮。 So that it won't let the user do multiple clicks before the processing is done. 因此,在处理完成之前,它不会让用户多次点击。 When its done gathering the information and sending if off to your php page it submits the form it its action property. 当它完成收集信息并发送到你的php页面时,它将表单提交给它的action属性。

In this example I grab the value from an input that has the name property set to firstName , this value will be sent over to my PHP script and because I chose a type of POST i can see it in my as 在这个例子中,我从name属性设置为firstName的输入中获取值,该值将被发送到我的PHP脚本,因为我选择了一种POST我可以在我的POST看到它

$_POST['firstName']

to debug your post paramters to see what the data looks like so this in your receiving PHP script 调试你的帖子参数以查看数据的样子,以便在接收PHP脚本中进行调整

echo '<pre>', print_r($_POST, true), '</pre>';

this will give you a display of the data captured. 这将显示捕获的数据。

<script type="text/javascript">

$(document).ready(function() {


  $('.formSubmitButton').on('click', function(e) {

    e.preventDefault();
    if (!$('.formSubmitButton').hasClass('submitted'))
      {
        // disable the form to prevent multple clicks
        $('.formSubmitButton').addClass('submitted');

        $.ajax('/path/to/my/db/script', {

          type: 'post',
          data:{
             firstName: $('[name="firstName"]).val(),
             lastName: $('[name="lastName"]).val()
          }     
        }).done(function(data) {

            $('.parentForm').submit();

            // enable the form
            $('.formSubmitButton').removeClass('submitted');
        });
      }
      return false;
  });
});

</script>

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

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