0

I have a function that adds a new patient to the database, gets the json data back and then adds the new patient to the select menu. That works fine. I need to add that data into the corresponding text inputs i.e., input#patient_id, input#patient_firstname, etc. which should occur in the .ajax complete, but nothing happens.

Would appreciate any help!!

Ajax

$('#newpatient').click(function() {
var patientadd = $('#patientaddform').serializeArray();
  $.ajax({
      url:      'adam.php',
      type:     "POST",
      data:     patientadd,
      dataType: 'json',
      success:  function(data){
                    var html = '';
                    var len  = data.length;
                    for (var i = 0; i< len; i++) {
                    html += '<option selected="selected" value="' + data[i].patient_id + '">' + data[i].patient_firstname + ' ' + data[i].patient_lastname + ' : ' + data[i].patient_dob + '</option>';
                    }
                    alert(html);
                    $('#patientselect').prepend(html);
                    $('#patientselect').selectmenu('refresh', true);
                }, 
      complete: function (data) {
                   for(var id in data) {        
                          $('input#' + id).val( data[id] );
                   }
                }     

  });   

});

json data returned

[{"patient_id":"22","patient_firstname":"","patient_lastname":"","patient_dob":"0000-00-00"}]

the corresponding text input fields are input#patient_id, input#patient_firstname, etc, that is why I'm trying to add the 'input#' to the id in the ajax complete and then add that val to the text input.

2 Answers 2

1

The params for the complete call back are jqXHR and textStatus. Not data. See http://api.jquery.com/jQuery.ajax/ for the correct definition of complete(jqXHR, textStatus)

Sign up to request clarification or add additional context in comments.

2 Comments

Should I move the function to the ajax success?
I moved the function to success and used user28061 suggestion and it works perfectly, THANK YOU!!!
0

Your json data is an array of objects, so you have to add take the first element of the array like this:

for(var id in data[0]) {        
  $('input#' + id).val( data[0][id] );
}

Or you have to adapt the server response.

Also consider @techfoobar's answer:

The params for the complete call back are jqXHR and textStatus. Not data. See http://api.jquery.com/jQuery.ajax/ for the correct definition of complete(jqXHR, textStatus)

2 Comments

I see why this should work, but my text inputs are not showing the values. Any thoughts?
@ user28061 It works perfectly! I also had to move the function the the ajax success. Thank you, I really appreciate your help!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.