我有一个带有用户名和密码的 HTML 表单。单击提交按钮后,我想在两个不同的服务器上发布到两个不同的 PHP 方法。如何才能做到这一点?这可以使用 HTML 表单完成吗?或者我应该使用 JavaScript?有什么建议吗?
更新
我尝试了以下代码,但它不起作用。我哪里错了?提前谢谢你..!
<!DOCTYPE html><!--HTML5 doctype-->
<html>
<head>
<title id="login_form">Login Form testing</title>
<script type="text/javascript" src="//code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$('#loginform').submit(function(){
$("form" ).on( "submit", function( event ) {
event.preventDefault(); // prevent the default submission behavior
var data = $(this).serialize(); // gather data from the form and serialize it
$.post('URL_1', data, successAction1); // post it to destination 1
$.post('URL_2', data, successAction2); // post it to destination 2
});
var successAction1 = function() { alert("Success one");};
var successAction2 = function() { alert("Success two");};
});
</script>
</head>
<body>
<form id="loginform" action="">
<fieldset >
<legend>Login</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >UserName*:</label>
<input type='text' name='username' id='username' maxlength="50" />
<label for='password' >Password*:</label>
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>
</body>
最佳答案
您可以使用 AJAX 发布它,例如。 JQuery图书馆:
$( "form" ).on( "submit", function( event ) { // listen to form submission (use whatever selector suits you, but make sure it is the form itself)
event.preventDefault(); // prevent the default submission behavior
var data = $(this).serialize(); // gather data from the form and serialize it
$.post('URL_1', data, successAction1); // post it to destination 1
$.post('URL_2', data, successAction2); // post it to destination 2
});
var successAction1 = function() { /*what to do after 1st post*/};
var successAction2 = function() { /*what to do after 2nd post*/};
我建议阅读关于 $.serialize 的 api 文档和 $.post
关于javascript - 从单一表单发布到两种不同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17210658/