javascript - AJAX POST Methods In HTML -
i trying update server database using ajax calls 2-dimensional array each time add more fields, ignores first fields updates last field. can please? in case phonework field being updated.
function update_user(){ //get form data //there many ways data using jquery (you can use 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!'); } }); }
you code not updating first field bug updating last field, because using same name 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 other name 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'),
Comments
Post a Comment