你好,我有一个这样开始的表单:
<form id="SignupForm" action="results.php" method="post" accept-charset="utf-8" class="form-vertical">
表单部分中输入的值需要在results.php中进行处理。
然而,我还需要通过API将表单部分中的字段值传递给第三方应用程序,如下所示:
<form accept-charset="utf-8" action="https://thirdpartysite.com/apikey"
method="post">
我该怎么做?
表单元素不是设计来发送它的数据到多个动作,唯一的方法是将您的数据发送到api在您的results.php
与php或您发送您的数据后提交被javascript触发,然后提交表单。
$('#SignupForm').on('submit', function(e){
// don't e.preventDefault - let the ajax event be async false
// so your form gets submited after the ajax request was finished
var self = $(this);
//send to api wait for response
$.ajax({
type: "POST",
url: 'https://thirdpartysite.com/apikey',
data: self.serialize(),
async: false
});
});
您可以使用javascript拦截表单提交事件,然后将信息发布到您自己的脚本和第三方脚本。
一个快速的(未经测试的)jquery示例:$('myForm').on('submit', function (e) {
// prevent the form from submitting
e.preventDefault();
var formData = {}; // This would be the actual form data
// post to your script
$.post('/myscript.php', formData);
// post to third party
$.post('thirdPartySite', formData);
});
您可以在results.php中放入CURL请求,将值发送到https://thirdpartysite.com/apikey