" /> 我想将相同的数据提交到两个 url ..-6ren">
gpt4 book ai didi

php - 通过javascript提交两个表单而无需点击

转载 作者:行者123 更新时间:2023-11-30 13:23:36 24 4
gpt4 key购买 nike

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

<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>

我想将相同的数据提交到两个 url ...

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 自动发送到两个 url

最佳答案

您可以使用 jQuerys ajax 并通过 post 将数据发送到两个 url。

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

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

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

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

然后使用 ajax 将表单数据发送到两个 url,作为 post 类型:

$.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;
});
});

关于php - 通过javascript提交两个表单而无需点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com