POST AJAX Methods in HTML
I'm trying to update the server database using Ajax calls for a 2D array, but every time I add more fields, it ignores the first fields, but only updates the last field. Can anyone please help? Therefore, in this case, only the phonework field is updated.
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!');
}
});
}
+3
source to share
2 answers
You are not updating the last field by updating the last field because you are using the same name for all fields users[0][customfields][0][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'),
Try using a different name for your fields like
'users[0][customfields][0][type]': 'stopcity',
'users[0][customfields][0][value]':document.getElementById('city1').value,
'users[0][customfields][1][type]': 'postalcode',
'users[0][customfields][1][value]':document.getElementById('postc').value,
'users[0][customfields][2][type]': 'province',
'users[0][customfields][2][value]': document.getElementById('prov').value,
'users[0][customfields][3][type]': 'stopcountry',
'users[0][customfields][3][value]': document.getElementById('country2').value,
'users[0][customfields][4][type]': 'addressline1',
'users[0][customfields][4][value]': document.getElementById('1').value,
'users[0][customfields][5][type]': 'addressline2',
'users[0][customfields][5][value]': document.getElementById('2').value,
'users[0][customfields][6][type]': 'phonemobile',
'users[0][customfields][6][value]': $("#mobile").attr('value'),
'users[0][customfields][7][type]': 'phonework',
'users[0][customfields][7][value]': $("#work").attr('value'),
+2
source to share
The problem is you are assigning all types and values ββto one element of the array
users[0][customfields][0][type]
^
Thus, you need to increase this index for every new type / value pair. Or you can use actual arrays / objects instead.
data: {
'wstoken': '**************',
'moodlewsrestformat': 'json',
'wsfunction': 'core_user_update_users',
'users': [{
id:'2328',
firstname: document.getElementById('name').value,
lastname: document.getElementById('surname').value,
customfields: [
{ type:'stopcity', value: document.getElementById('city1').value },
{ type: 'postalcode', value: document.getElementById('postc').value },
{ type: 'province', value: document.getElementById('prov').value },
{ type: 'stopcountry', value: document.getElementById('country2').value },
{ type: 'addressline1', value: document.getElementById('1').value },
{ type: 'addressline2', value: document.getElementById('2').value },
{ type: 'phonemobile', value: $("#mobile").attr('value') },
{ type: 'phonework', value: $("#work").attr('value') }
]
}]
}
0
source to share