在我下面的代码中,你可以看到'$_SERVER['REQUEST_URI']'ContactCreate.php',这是我需要发送单个表单的两个操作。我不知道如何才能完成这项工作。
提前感谢大家
$(function() {
$("#myform").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: '$_SERVER['REQUEST_URI']' 'ContactCreate.php',
type: 'POST',
data: $(this).serialize(),
beforeSend: function() {
$("#message").html("sending...");
},
success: function(data) {
$("#message").hide();
$("#response").html(data);
}
});
});
});
您将需要2个ajax调用。您可以等待两者完成使用$.when
方法并执行中的公共逻辑
$(function() {
$("#myform").on("submit", function(e) {
e.preventDefault();
$("#message").html("sending...");
var formSerialized = $(this).serialize();
var ajaxCall1 = $.post('$_SERVER['REQUEST_URI']', formSerialized);
var ajaxCall2 = $.post('ContactCreate.php', formSerialized);
$.when( ajaxCall1, ajaxCall2).done(function (v1, v2) {
$("#message").hide();
// your logic when both ajax request finished
});
});
});
还不确定$_SERVER['REQUEST_URI']
是否会解析到Javascript上的任何内容,这将取决于该代码的位置。
您必须使用REQUEST_URI 的JS模拟
var request_uri = location.pathname + location.search;
在你的代码-
$(function() {
$("#myform").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: location.pathname + location.search+ 'ContactCreate.php',
type: 'POST',
data: $(this).serialize(),
beforeSend: function() {
$("#message").html("sending...");
},
success: function(data) {
$("#message").hide();
$("#response").html(data);
}
});
});
});
只需将其相邻添加即可:
$(function() {
$("#myform").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: '<?php echo $_SERVER['REQUEST_URI']; ?>' ,
type: 'POST',
data: $(this).serialize(),
beforeSend: function() {
$("#message").html("sending...");
},
success: function(data) {
$("#message").hide();
$("#response").html(data);
}
});
$.ajax({
url: 'ContactCreate.php',
type: 'POST',
data: $(this).serialize(),
beforeSend: function() {
$("#message").html("sending...");
},
success: function(data) {
$("#message").hide();
$("#response").html(data);
}
});
});
});