0

I am using jQuery ajax function to get database values code and subcode.Whenever I get the code and subcode I have to get the name stored in another table corresponding to code and subcode. First function i am using is

function loadSecretaries()
{
    var items = "";
    $.getJSON("Tac_members_GetTacSecretaries.php", function(data) {

        $.each(data, function(index, item) {
            var name = getSecretariesName(item.ECODE, item.ECODE_S);
            items += "<option value='" + item.ECODE + "'>" + item.ECODE_S + </option>";    
        });
        $("#select_reviewer_secretary").html(items);
    });

}

The second function is

function getSecretariesName(code, subcode)
{

    var items = "";
    $.ajax({
        async: false,
        url: '../PhpProject1/Tac_Members_GetReviewerNames.php',
        dataType: 'json',
        type: 'POST',
        data: {"code": code, "subcode": subcode},
        success: function(response) {
            alert(response.ename) ;
           var name =response.ename;
        //here i want to return ename to first function
        },
        error: function(x, e) {
            if (x.status === 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status === 404) {
                alert('Requested URL not found.');
            } else if (x.status === 500) {
                alert('Internel Server Error - Please try after relogin to the application');
            } else if (e === 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e === 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }

    });
return name;
}

I am getting undefined when alert 'name' in first function. How to solve this. Thanks in advance.

kinakuta
  • 9,029
  • 1
  • 39
  • 48
Shahanas
  • 25
  • 1
  • 1
  • 8
  • what does your response look like from the ajax call in your dev tools? – kinakuta Sep 29 '14 at 06:05
  • i getting code and subcode.but i am not getting name.i am trying to this for loading name into select option instead of code and subcode – Shahanas Sep 29 '14 at 06:06
  • the issue, then, sounds like a problem with the ajax endpoint you're calling - if the property isn't being returned, then you should either review the documentation for the endpoint or, if you're responsible for that endpoint, debug the endpoint – kinakuta Sep 29 '14 at 06:07
  • 1
    can u please show us your php function? – aM-Vee Sep 29 '14 at 06:08
  • i getting name in response.ename.but i cant return it to first function – Shahanas Sep 29 '14 at 06:09

1 Answers1

0

try this

   function getSecretariesName(code, subcode)
{

    var items = "";
    var name = "";
    $.ajax({
        async: false,
        url: '../PhpProject1/Tac_Members_GetReviewerNames.php',
        dataType: 'json',
        type: 'POST',
        data: {"code": code, "subcode": subcode},
        success: function(response) {
            alert(response.ename) ;
           name = response.ename;
        //here i want to return ename to first function
        },
        error: function(x, e) {
            if (x.status === 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status === 404) {
                alert('Requested URL not found.');
            } else if (x.status === 500) {
                alert('Internel Server Error - Please try after relogin to the application');
            } else if (e === 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e === 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }

    });
return name;
}
Sumeet Patil
  • 422
  • 3
  • 14