1

我有一个像这样的html表单:

<form action="myServer.com/test.php" method="post">
...
</form>

提交表单后,用户应该被重定向到 myServer.com/test.php 并且 ofc 将发布数据发送到脚本。但同时(或之前),我想将相同的 POST 数据发布到另一个脚本“myServer.com/test2.php”。

$.post("myServer.com/test2.php", variableWithTheFormPOSTData);

我尝试将 EventListener 附加到表单提交,但这似乎不起作用,也许 jquery 帖子在重定向之前无法足够快地提交?

我希望你能帮助我。:(

4

3 回答 3

0

每当您使用带有 onsubmit 的方法时,请确保它返回 true,否则将不会提交。

于 2013-09-03T16:50:59.123 回答
0

您可以使用 ajax 本身来完成,这将避免重新加载页面

<form id="form1">
 .....
<input type="text" name="email"/> <!--Example input text box-->
<input type="button" id="submit"/> 
</form>

和jQuery代码

$("#submit").click(function()
{
$.post("myServer.com/test2.php", $("#form1").serialize());//$("#form1).serialize() will get the data in the form automatically
$.post("myServer.com/test.php", $("#form1").serialize());

});

.serialize()将自动序列化要发布的表单中的数据

在您的服务器端页面中使用它

<?php
parse_str($_POST['serialize'], $data);
$name  = $data["email"] 


// do your code
?>

希望这有帮助,谢谢

于 2013-09-03T17:17:28.217 回答
0

use a button as your submit button sothat the form doesnt get posted at the same time you click on it. And trigger the ajax function to post the data indirectly to the second page.

<form name="myform" action="myServer.com/test.php" method="post">
    ...
    <button onclick="doIndirectPost();"></button>
</form>

and in the success callback of ajax posting function trigger your form post

function doIndirectPost() {
    //initialize variableWithTheFormPOSTData here
    $.post("myServer.com/test2.php", variableWithTheFormPOSTData,function(success,error)    {
         //ajax post is completed now we trigger the form post to test.php
         document.myform.submit();

    });  
}
于 2013-09-03T16:52:30.380 回答