Ajax 表单提交到两个脚本
Ajax form submitting to two scripts
>基本上我有一个表单,我想将变量传递给两个单独的 php 脚本,以便在提交时进行处理。我所做的工作似乎适合我的目的,我只是想知道是否有办法在一个 jquery 脚本中执行此操作,而不必像我所做的那样重复脚本并更改 URL。还是 iv 的方式很好?在谷歌上找不到很多这种用法的例子,但也许我没有搜索正确的东西。另外,如果我正在做一些不好的做法,请告诉我,我对Ajax很陌生,我遵循的每个教程似乎都以不同的方式做事。
<script>
$(document).ready(function(){
$('#sform').submit(function(){
// show that something is loading
$('#response').html("<b>Loading response...</b>");
/*
* 'post_receiver.php' - where you will pass the form data
* $(this).serialize() - to easily read form data
* function(data){... - data contains the response from post_receiver.php
*/
$.ajax({
type: 'POST',
url: 'move_to_lease.php',
data: $(this).serialize()
})
.done(function(data){
// show the response
$('#response').html(data);
})
.fail(function() {
// just in case posting your form failed
alert( "Posting failed." );
});
// to prevent refreshing the whole page page
return false;
});
});
</script>
<script>
$(document).ready(function(){
$('#sform').submit(function(){
// show that something is loading
$('#txtHint').html("<b>Loading response...</b>");
/*
* 'post_receiver.php' - where you will pass the form data
* $(this).serialize() - to easily read form data
* function(data){... - data contains the response from post_receiver.php
*/
$.ajax({
type: 'POST',
url: 'show_lease_sub.php',
data: $(this).serialize()
})
.done(function(data){
// show the response
$('#txtHint').html(data);
})
.fail(function() {
// just in case posting your form failed
alert( "Posting failed." );
});
// to prevent refreshing the whole page page
return false;
});
});
</script>
最佳做法是使用一个后端脚本处理这两个调用。您可以使用数组返回 2 个单独的响应。提示 - 编写一个类。
阿贾克斯.php
class leaseStuff{
public static function moveToLease($data){
// add code from move_to_lease.php, change $_POST to $data
}
public static function showLeaseSub($data){
// add code from show_lease_sub.php, change $_POST to $data
}
}
echo json_encode(array(
"moveToLease"=>leaseStuff::moveToLease($_POST),
"showLeaseSub"=>leaseStuff::showLeaseSub($_POST)
));
然后,您可以将 Ajax 合并为一个调用:
$.ajax({
type: 'POST',
url: 'ajax.php',
data: $(this).serialize()
}).done(function(data){
data = JSON.parse(data);
$('#response').html(data.moveToLease);
$('#txtHint').html(data.showLeaseSub);
});
尝试使用您的 URL 遍历数组:
$('#sform').submit(function () {
var urls = [
{
"url": "move_to_lease.php",
"response_id": "#response"
},
{
"url": "show_lease_sub.php",
"response_id": "#txtHint"
}
]
for (var k in urls) {
$(urls[k].response_id).html("<b>Loading response...</b>");
$.ajax({
type: 'POST',
url: urls[k].url,
data: $(this).serialize()
}).done(function (data) {
$(urls[k].response_id).html(data);
}).fail(function () {
alert("Posting failed.");
});
}
return false;
});
更新:
根据您提供的映像 IMAGE,您需要进行两个不同的 AJAX 调用。
提交表单时:->将信息保存到数据库->获取信息以更新显示
更改选择下拉列表时:->从数据库获取信息->获取信息以更新显示
相关文章:
- 在同一页上的两个脚本中使用全局变量
- JavaScript - 需要帮助组合两个脚本
- 加载两个脚本块(使用lab.js作为加载管理器)
- 如何使用jquery或vanillajavascript从iframe中获取两个脚本src
- 当我在一个html页面上添加两个脚本时,其中一个不起作用
- 在 HTML 上运行两个脚本(Java 脚本)
- 原型:这两个脚本之间的区别
- 两个脚本不会同时工作
- 组合两个脚本以在所有浏览器上保存Google图表时出现问题
- 如何合并两个脚本并同时触发它们
- jQuery结合了两个脚本来实现对锚点的平滑滚动
- 如何链接两个脚本
- 两个脚本出现Jade渲染错误,未捕获语法错误:意外的令牌<
- 单击来自同一元素上两个脚本的事件
- 使用jquery.on()方法处理同一元素(选项标记)的两个脚本之间的冲突
- 同一页面上的两个表单和两个脚本,第一个脚本将被调用而不考虑提交
- 使用服务器从两个脚本发送的事件
- 为什么'我的两个脚本都不能在html中工作
- 两个脚本(FitVids.js,Vimeo.js)一起杀死调用一个函数来播放视频,导致:VimeoAPI不是一个函数
- 是否可以让HTML中的一个链接执行两个脚本