我在merging
数组中遇到了一些问题“$_POST
”。
我正在使用AJAX以便在单击按钮时交换div的元素,但是我在其中也有一个表单,其值被ajax覆盖。 (对不起的解释感到抱歉;我是初学者!)
所以我有这个jquery代码:
$.post(url.concat("/profileajax.php"), {
contentVar: cv
}, function(data) {
$("#myDiv").html(data).show();
})
但是由于此代码,我的$_POST
数组仅包含contentVar
元素。我想要做的是让函数保持当前的$_POST
元素并在它们之后添加contentVar
元素。
像
$.post(url.concat("/profileajax.php"), { /*Insert values gotten from the form here */ , contentVar: cv
}, function(data) {
$("#myDiv").html(data).show();
})
答案 0 :(得分:0)
$('#myForm').on('submit', function (event) {
event.preventDefault();
var data = $('#myForm').serializeArray();
var cv = "cvdata";
data.push({name: 'contentVar', value: cv});
var url = "http://example.com";
$.ajax({
type: "POST",
url: url.concat("/profileajax.php"),
data: data,
success: function (data) {
$("#myDiv").html(data).show();
}
});
});