【发布时间】:2015-10-13 16:20:02
【问题描述】:
我正在尝试使用二维数组的 Ajax 调用更新服务器数据库,但每次添加更多字段时,它都会忽略第一个字段,而只更新最后一个字段。有人可以帮忙吗?所以在这种情况下,只有 phonework 字段正在更新。
function Update_user(){
//Get the form data
//There are many ways to get this data using jQuery (you can use the class or id also)
$.ajax({
jsonp: 'jsoncallback',
dataType: 'json',
type: 'POST',
cache:false,
beforeSend: function() {$.mobile.loading('show')},
complete: function() {$.mobile.loading('hide')},
crossDomain: true,
url: 'https://testing.vle.getsmarter.co.za/webservice/rest/server.php',
data: {
'wstoken': '**************',
'moodlewsrestformat': 'json',
'wsfunction': 'core_user_update_users',
'users[0][id]': '2328',
'users[0][firstname]': document.getElementById('name').value,
'users[0][lastname]': document.getElementById('surname').value,
'users[0][customfields][0][type]': 'stopcity',
'users[0][customfields][0][value]':document.getElementById('city1').value,
'users[0][customfields][0][type]': 'postalcode',
'users[0][customfields][0][value]':document.getElementById('postc').value,
'users[0][customfields][0][type]': 'province',
'users[0][customfields][0][value]': document.getElementById('prov').value,
'users[0][customfields][0][type]': 'stopcountry',
'users[0][customfields][0][value]': document.getElementById('country2').value,
'users[0][customfields][0][type]': 'addressline1',
'users[0][customfields][0][value]': document.getElementById('1').value,
'users[0][customfields][0][type]': 'addressline2',
'users[0][customfields][0][value]': document.getElementById('2').value,
'users[0][customfields][0][type]': 'phonemobile',
'users[0][customfields][0][value]': $("#mobile").attr('value'),
'users[0][customfields][0][type]': 'phonework',
'users[0][customfields][0][value]': $("#work").attr('value'),
},
success: function(data) {
// enable previous buttons
$('#enable').css('visibility', 'visible');
$('#back1').css('visibility', 'visible');
// disable previous buttons
$('#save').css('visibility', 'hidden');
$('#cancel').css('visibility', 'hidden');
// diable fields
window.location.reload();
alert("Profile updated." );
},
error: function() {
alert('Update has failed!');
}
});
}
【问题讨论】:
-
请正确格式化您的代码。这只会让我们远离你的问题。
-
我很抱歉。这是我的第一篇文章。
标签: javascript ajax json html multidimensional-array