通过javascript提交两个表单,无需点击

时间:2012-02-18 22:07:09

标签: php javascript forms function post

我使用以下代码自动提交表单

<script type="text/javascript">
$(function () {
    $('#submit').click();
});
</script>

<div style="display:none">
<form method="post" action="http://mydomain/texting/register.php?list_id=15">
<input type="hidden" name="name" value="<?php echo $_SESSION['name2']; ?>" />
<input type="hidden" name="email" value="<?php echo $_SESSION['email2']; ?>" />
<input type="hidden" name="ok" value="1" />
<input type="submit" id="submit" value="Sign Up" style="visibility:hidden;" />
</form>
</div>

我想将相同的数据提交给两个网址......

http://mydomain/texting/register.php?list_id=15
http://mydomain.com/texting/register.php?list_id=<?php echo $_SESSION['city2']; ?>

如何编辑我的代码以自动提交相同的表单数据?

我试过这段代码

<script type="text/javascript">
function submitTwice(f){
f.action = 'http://mydomain.com/texting/register.php?list_id=<?php echo $_SESSION['city2']; ?>';
f.target='ifr1';
f.submit();
f.action = 'http://mydomain.com/texting/register.php?list_id=15';
f.target='ifr2';
f.submit();
}
</script>
*/


<script type="text/javascript">
$(function () {
    $('submitTwice(f)').click();
});
</script>


<div style="display:none">
<iframe name="ifr1" width="20" height="20">
<form method="post" action="http://mydomain.com/texting/register.php?list_id=<?php echo $_SESSION['city2']; ?>">
<input type="hidden" name="name" value="<?php echo $_SESSION['name2']; ?>" />
<input type="hidden" name="email" value="<?php echo $_SESSION['email2']; ?>" />
<input type="hidden" name="ok" value="1" />
<input type="submit" id="submit" value="Sign Up" style="visibility:hidden;" />
</form></iframe>
</div>
<div style="display:none"><iframe name="ifr2" width="20" height="20">
<form method="post" action="http://mydomain.com/texting/register.php?list_id=15">
<input type="hidden" name="name" value="<?php echo $_SESSION['name2']; ?>" />
<input type="hidden" name="email" value="<?php echo $_SESSION['email2']; ?>" />
<input type="hidden" name="ok" value="1" />
<input type="submit" id="submit" value="Sign Up" style="visibility:hidden;" />
</form></iframe>
</div>

但它无法正常工作,任何建议都要实现,相同的数据将使用javascript自动发送到两个网址

2 个答案:

答案 0 :(得分:2)

您可以使用jQuerys ajax并将数据发送到两个网址。

首先保存要在有效数据阵列中发送的所有数据。如果你想让它自动化,你可以这样做:

var sendData = {};
$("#formId").find("input").each(function(i, item){
    sendData[item.name] = item.value;
});

或作为一个简单的字符串:

var sendData = "name=value&email=value";

然后将表单数据发送到两个带有ajax的URL,作为帖子类型:

$.ajax({ url: "http://url1/", type: "POST", data: sendData,
         success: function(response){
             alert(response);
         }
});
$.ajax({ url: "http://url2/", type: "POST", data: sendData,
         success: function(response){
             alert(response);
         }
});

注意:确保将false返回到按钮或发送表单,您可以执行以下操作:

$("formId").submit(function(){
    // do ajax send data
    return false;
}

编辑:添加未经检查的代码,注释

$(document).ready(function(){
    // grab the form and bind the submit event
    $("#formId").submit(function(){
        // collect all the data you want to post
        var sendData = {};
        this.find("input").each(function(i, item){
            sendData[item.name] = item.value;
        });

        // send the request to both urls with ajax
        $.ajax({ url: "url1", type: "POST", data: sendData,
                 success: function(response){
                    // handle response from url1
                    alert(response);
                 }
        });
        $.ajax({ url: "url2", type: "POST", data: sendData,
                 success: function(response){
                    // handle response from url2
                    alert(response);
                 }
        });

        // return false, to disable default submit event
        return false;
    });
});

答案 1 :(得分:1)

只需以常规格式向服务器脚本发送姓名和电子邮件, 然后在您的register.php中执行两个请求,例如HttpRequest等......

一些手绘代码:

客户端:

<form id="autoSubmitForm" method="post" action="/register.php?list_id=15">
    <input type="hidden" name="name" value="<?php data['name2']; ?>" />
    <input type="hidden" name="email" value="<?php data['email2']; ?>" />
    <input type="submit" name="submit" value="Sign Up" style="visibility:hidden;" />
</form>
<script>
    $(document).ready(function(){
       $('#autoSubmitForm').submit();
    });
</script>

服务器:

<?php
   $name = $_POST["name2"];
   $email = $_POST["email2"];
   $url1 = 'http://lala'; //or relative to server root: '/lala'
   $url2 = 'http://other';
   sendRequest($url1, $name, $email);
   sendRequest($url2, $name, $email);
?>

sendRequest的实现值得另一个问题(或阅读上面提到的HttpRequest文档)

祝你好运,我看到你的重复问题被关闭了......