0

In my code I call a function which first of all displays a form and builds the select drop-down lists with the appropriate options. I then call the following function, passing it a case id. The function:

  1. calls another file that uses AJAX to connect to my database and returns a single row of data for the case id specified.

  2. checks if the ajax function has completed successfully

  3. if successful, it populates the form fields with the json data

This form is correctly populated but if I remove the alert code then it does not populate the form. Can anybody help?

function loadCase (caseID) {

$.get("./case_det/exist_case_det/ajax_get_exist_case_det.php?caseID=" + caseID, function(data, status) {

    alert (caseID + " now loading");

    if (status === "success") {
        json_data=JSON.parse(data);

        //save object to localStorage
        localStorage['my_case'] = JSON.stringify(json_data);

        // Populate Case Detail
        $('#categorisation').val(json_data[0].cat_id);
        $('#priority').val(json_data[0].priority_id);
        $('#type').val(json_data[0].type_id);
        $('#stage').val(json_data[0].stage_id);
        $('#summary').val(json_data[0].summary);
    } else {
        alert ("System encountered probl;ems laodign case data");
    }

});
}
Lee A
  • 45
  • 1
  • 8
  • Also tried: function loadCase (caseID) { $.ajax({ url : './case_det/exist_case_det/ajax_get_exist_case_det.php', type: 'POST', data: {caseID:caseID}, success : loadCaseSuccess, error: loadCaseError }); } //loadCase().done(loadCaseSuccess); function loadCaseSuccess(data, textStatus) { alert(textStatus); //alert(data); json_data=JSON.parse(data); alert(json_data[0].cat_id); // Populate Case Detail $('#categorisation').val(json_data[0].cat_id); } function loadCaseError() { alert ("System encountered problems loading case data"); } fails if I omit: alert(textStatus) – Lee A Sep 24 '15 at 17:15
  • Which version of jQuery are you using? – BA_Webimax Sep 24 '15 at 17:17

1 Answers1

0

Try doing it with the more recent jqXHR methods...

function loadCase (caseID) {

    $.get("./case_det/exist_case_det/ajax_get_exist_case_det.php?caseID=" + caseID )
    .done( function( data ) {

        json_data=JSON.parse(data);

        //save object to localStorage
        localStorage['my_case'] = JSON.stringify(json_data);

        // Populate Case Detail
        $('#categorisation').val(json_data[0].cat_id);
        $('#priority').val(json_data[0].priority_id);
        $('#type').val(json_data[0].type_id);
        $('#stage').val(json_data[0].stage_id);
        $('#summary').val(json_data[0].summary);

    })
    .fail( function() {
        alert ("System encountered problems loading case data");
    });
}
BA_Webimax
  • 2,651
  • 1
  • 11
  • 15
  • Hi. Thanks for your response but that didn't work. I also tried the long hand version of AJAX referenced in my previous comment and set asyn:false but again, no joy. Any further suggestions much appreciated. – Lee A Sep 24 '15 at 18:21
  • What do you get if you `console.log( data );` in the done() method – BA_Webimax Sep 24 '15 at 20:06
  • Hi. I get the following: [{"case_id":"23","person_id":"234313","response_deadline":"Contact member by 5pm today","member":"Lee Aldrich","date_created":"2015-09-24 08:29:18","created_by_user_id":"2","created_by":"Joe Bloggs","cat_id":"97","cat_desc":"Return to work","priority_id":"1","priority_desc":"High","stage_id":null,"stage_desc":null,"type_id":"2","type_desc":"General enquiry","summary":"hooray","rep_required":"1","meeting_date_time":null,"meeting_location":null,"pre_existing":null,"status_id":"5","status_desc":"Open"}] – Lee A Sep 24 '15 at 21:16
  • Are you using the PHP function `json_encode()` to prepare the data for transfer back? If so try skipping the JSON.parse() call and insert the data directly `$('#categorisation').val( data.cat_id );` – BA_Webimax Sep 25 '15 at 13:56